How to make Django serve static files with Gunicorn?

alix picture alix · Oct 9, 2012 · Viewed 64.1k times · Source

I want to run my django project under gunicorn on localhost. I installed and integrated gunicorn. When I run:

python manage.py run_gunicorn

It works but there are no any static files (css and js)

I disabled debug and template_debug in settings.py (made them false), but it is still same. Am I missing something?

I call statics like:

{{ STATIC_URL }}css/etc....

Answer

rantanplan picture rantanplan · Oct 9, 2012

When in development mode and when you are using some other server for local development add this to your url.py

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

# ... the rest of your URLconf goes here ...

urlpatterns += staticfiles_urlpatterns()

More info here

When in production you never, ever put gunicorn in front. Instead you use a server like nginx which dispatches requests to a pool of gunicorn workers and also serves the static files.

See here