Django Serving Static Files without a Web Server
By Rayed
UPDATE: Steps in this post isn’t correct, check the Django Themes post for better solution.
When deploying Django site it is always recommended to serve static files (e.g. js, css, img, static, media) using a normal web server instead of relying on Django built development server or Gunicorn.
But sometimes you want to test something quick and you don’t want to bother with installing or configuring a web server, the solution is to use the built-in django.views.static.serve view to serve the static files from Django it self:
$ vi project/urls.py
:
# Serve Static Files
from django.conf import settings
from django.conf.urls.static import static
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += patterns('django.views.static',
url(r'^(?P<path>(js|css|img)/.*)$', 'serve', {'document_root': settings.BASE_DIR+'/../www'}),
)
Notice it is configured to work on DEBUG mode only since it isn’t efficient nor tested for production use.