django staticfiles at url root django staticfiles at url root django django

django staticfiles at url root


You can manually add extra locations that do not exist within the static directories within your project:

urls.py

from django.conf import settingsfrom django.conf.urls.static import staticurlpatterns = patterns('',    # ... the rest of your URLconf goes here ...)if settings.DEBUG:    urlpatterns += static('/css/', document_root='app_root/path/to/css/')    urlpatterns += static('/images/', document_root='app_root/path/to/images/')    urlpatterns += static('/js/', document_root='app_root/path/to/js/')

This will map the media for the DEBUG dev server. And when you are running your production mode server, you will obviously be handling these static locations from the web server instead of sending the request through to django.


why not keep the staticfiles functionality and simply use rewrites at the web server level to serve up the content.

for instance:

rewrite /css /static permanent; (for nginx) 

this would keep your project directory a lot cleaner and also make it easier to move your static directories around in the future, for instance to move to your STATIC_URL to a CDN.


This is how you set up your urls.py to serve both index.html and your other static files on / in Django 1.10 (while still being able to serve other Django views):

from django.contrib.staticfiles.views import servefrom django.views.generic import RedirectViewurlpatterns = [    # / routes to index.html    url(r'^$', serve,        kwargs={'path': 'index.html'}),    # static files (*.css, *.js, *.jpg etc.) served on /    url(r'^(?!/static/.*)(?P<path>.*\..*)$',        RedirectView.as_view(url='/static/%(path)s')),]

See this answer where I wrote a more complete explanation of such a configuration – especially if you want to use it for production.