7
If you are using two different domains. Here is a git seed for how I do it. Feel free to use it.
.
βββ app/
β βββ shared/ // acts as reusable components or partials of our site
β β βββ sidebar/
β β β βββ sidebarDirective.js
β β β βββ sidebarView.html
β β βββ article/
β β βββ articleDirective.js
β β βββ articleView.html
β βββ components/ // each component is treated as a mini Angular app
β β βββ home/
β β β βββ homeController.js
β β β βββ homeService.js
β β β βββ homeView.html
β β βββ blog/
β β βββ blogController.js
β β βββ blogService.js
β β βββ blogView.html
β βββ app.module.js
β βββ app.routes.js
βββ assets/
β βββ img/ // Images and icons for your app
β βββ css/ // All styles and style related files (SCSS or LESS files)
β βββ js/ // JavaScript files written for your app that are not for angular
β βββ libs/ // Third party libraries such as jQuery, Moment, Underscore, etc.
βββ index.html
16
Hereβs how I approached this. Others have advocated completely decoupling your django and angularjs applications but this might work for you.
You have two apps, Account App and Other App. You want to create modular angular applications in both that can be βpluggedβ into another django project (with minimal modifications).
Account App static file structure:
β βββ static β β βββ app β β βββ js β β βββ apps β β β βββ accountApp.js β β βββ controllers β β β βββ accountControllers.js β β βββ directives β β β βββ accountDirectives.js β β βββ filters β β β βββ accountFilters.js β β βββ services β β βββ accountServices.js
Other App static file structure:
β βββ static β β βββ app β β βββ js β β βββ apps β β β βββ otherApp.js β β βββ controllers β β β βββ otherControllers.js β β βββ directives β β β βββ otherDirectives.js β β βββ filters β β β βββ otherFilters.js β β βββ services β β βββ otherServices.js
App Base file structure:
β βββ static β β βββ app β β β βββ js β β β β βββ app.js β β β β βββ controllers.js β β β β βββ directives.js β β β β βββ filters.js β β β β βββ services.js
Main project static file folder (after running manage.py collectstatic):
β βββ static β β βββ app β β βββ js β β βββ app.js β β βββ controllers.js β β βββ directives.js β β βββ filters.js β β βββ services.js β β βββ apps β β β βββ accountApp.js β β β βββ otherApp.js β β βββ controllers β β β βββ accountControllers.js β β β βββ otherControllers.js β β βββ directives β β β βββ accountDirectives.js β β β βββ otherDirectives.js β β βββ filters β β β βββ accountFilters.js β β β βββ otherFilters.js β β βββ services β β βββ accountServices.js β β βββ otherServices.js
Instead of using just regular AngularJS templates, use Django-powered AngularJS templates so you can pass cool stuff when rendering angular templates, like django-crispy-forms or render entire app views with django then only modify them with angular.
Partials Django-controllers directory inside any angular app (eg Account App or Other App):
β βββ partials β β βββ __init__.py β β βββ urls.py β β βββ views.py
urls.py
urlpatterns = patterns('users.partials.views', url(r'^list/$', UserPartialListView.as_view(), name="list"), url(r'^detail/$', UserPartialDetailView.as_view(), name="detail"), )
views.py
# can pass any global context or methods here from app_base.views import PartialView # pass app global context or methods here class UserPartialView(PartialView): template_name = "users/partials/base.html" # view specific context or methods here class UserPartialListView(UserPartialView): template_name = "users/partials/list.html" # view specific context or methods here class UserPartialDetailView(UserPartialView): template_name = "users/partials/detail.html"
Partials Templates directory inside any angular app (eg Account App or Other App):
β βββ templates β β βββ accounts β β βββ partials β β βββ base.html β β βββ detail.html β β βββ list.html
Main Partials Django-router:
# myapp.partials.urls urlpatterns = patterns('', url(r'^accounts/', include('accounts.partials.urls', namespace="accounts_partials")), url(r'^others/', include('others.partials.urls', namespace="others_partials")), )
Full Example Directory Structure:
βββ accounts β βββ __init__.py β βββ forms.py β βββ management β β βββ __init__.py β β βββ commands β β βββ __init__.py β β βββ importbusinesses.py β βββ models.py β βββ partials β β βββ __init__.py β β βββ urls.py β β βββ views.py β βββ permissions.py β βββ serializers.py β βββ static β β βββ app β β βββ js β β βββ apps β β β βββ accountApp.js β β βββ controllers β β β βββ accountControllers.js β β βββ directives β β β βββ accountDirectives.js β β βββ filters β β β βββ accountFilters.js β β βββ services β β βββ accountServices.js β βββ templates β β βββ accounts β β βββ partials β β βββ base.html β β βββ detail.html β β βββ list.html β βββ urls.py β βββ views.py βββ api_root β βββ __init__.py β βββ admin.py β βββ models.py β βββ tests.py β βββ urls.py β βββ views.py βββ app_base β βββ __init__.py β βββ __init__.pyc β βββ forms.py β βββ models.py β βββ models.pyc β βββ static β β βββ LICENSE β β βββ README.md β β βββ app β β β βββ css β β β β βββ app.css β β β βββ img β β β βββ js β β β β βββ app.js β β β β βββ apps β β β β β βββ accountApp.js β β β β βββ controllers β β β β βββ controllers.js β β β β βββ directives β β β β βββ directives.js β β β β βββ filters β β β β βββ filters.js β β β β βββ services β β β β βββ services.js β β β βββ lib β β β β βββ angular β β β β βββ angular-animate.js β β β β βββ angular-animate.min.js β β β β βββ angular-animate.min.js.map β β β β βββ angular-cookies.js β β β β βββ angular-cookies.min.js β β β β βββ angular-cookies.min.js.map β β β β βββ angular-csp.css β β β β βββ angular-loader.js β β β β βββ angular-loader.min.js β β β β βββ angular-loader.min.js.map β β β β βββ angular-resource.js β β β β βββ angular-resource.min.js β β β β βββ angular-resource.min.js.map β β β β βββ angular-route.js β β β β βββ angular-route.min.js β β β β βββ angular-route.min.js.map β β β β βββ angular-sanitize.js β β β β βββ angular-sanitize.min.js β β β β βββ angular-sanitize.min.js.map β β β β βββ angular-scenario.js β β β β βββ angular-touch.js β β β β βββ angular-touch.min.js β β β β βββ angular-touch.min.js.map β β β β βββ angular.js β β β β βββ angular.min.js β β β β βββ angular.min.js.gzip β β β β βββ angular.min.js.map β β β β βββ errors.json β β β β βββ i18n β β β β β βββ ... β β β β βββ version.json β β β β βββ version.txt β β β βββ partials β β β βββ partial1.html β β β βββ partial2.html β β βββ config β β β βββ karma.conf.js β β β βββ protractor-conf.js β β βββ logs β β βββ package.json β β βββ scripts β β β βββ e2e-test.bat β β β βββ e2e-test.sh β β β βββ test-all.sh β β β βββ test.bat β β β βββ test.sh β β β βββ update-angular.sh β β β βββ watchr.rb β β β βββ web-server.js β β βββ test β β βββ e2e β β β βββ scenarios.js β β βββ lib β β β βββ angular β β β βββ angular-mocks.js β β β βββ version.txt β β βββ unit β β βββ controllersSpec.js β β βββ directivesSpec.js β β βββ filtersSpec.js β β βββ servicesSpec.js β βββ templates β β βββ app_base β β βββ base.html β β βββ index.html β βββ urls.py β βββ views.py
- [Django]-Django: Filter a Queryset made of unions not working
- [Django]-Pytest.mark.parametrize with django.test.SimpleTestCase
- [Django]-How to force application version on AWS Elastic Beanstalk