Search code examples
pythondjangogunicorn

How to make Django serve static files with Gunicorn?


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....

Solution

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

    from django.contrib.staticfiles.urls import staticfiles_urlpatterns
    
    # ... the rest of your URLconf goes here ...
    
    urlpatterns += staticfiles_urlpatterns()
    

    Note that staticfiles_urlpatterns() will only work when DEBUG = True is set in your settings.py.

    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