How to make Django serve static files with Gunicorn?

前端 未结 6 1239
小鲜肉
小鲜肉 2020-11-30 20:27

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

python manage.py run_gunicorn

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 21:27

    I've used this for my development environment (which uses gunicorn):

    from django.conf import settings
    from django.contrib.staticfiles.handlers import StaticFilesHandler
    from django.core.wsgi import get_wsgi_application
    
    
    if settings.DEBUG:
        application = StaticFilesHandler(get_wsgi_application())
    else:
        application = get_wsgi_application()
    

    And then run gunicorn myapp.wsgi. This works similar to @rantanplan's answer, however, it does not run any middleware when running static files.

提交回复
热议问题