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