[Answered ]-What is the right way to setup urls.py for django-rest-framework + angular-route + static files?

2👍

For development, I use something like this, at then end of the main urls.py file, to serve static files from various directories:

if settings.DEBUG:
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns
    from django.conf.urls.static import static

    # this connects STATIC_URL to STATIC_ROOT
    urlpatterns += staticfiles_urlpatterns()

    # connect other paths to directories
    urlpatterns += static(settings.MEDIA_URL, 
        document_root=settings.MEDIA_ROOT)
    urlpatterns += static('/app/', 
        document_root=join(settings.BASE_DIR, '../ng_app/'))

For production, you set the webserver to handle the requests. First the known routes like /admin/, /api/, /api-auth/ and files in /static/.

And then a catch-all route / that would always serve your index.html with the Angular app for any route. So that HTML5 routes in Angular will always load the app first, and the app then uses its own routes to build the correct page from API data.

The actual app files could be anywhere, even on a different server.

👤C14L

Leave a comment