How to make Django serve static files with Gunicorn?

不羁的心 提交于 2019-12-28 02:29:04

问题


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

回答1:


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




回答2:


Whitenoise

Post v4.0

http://whitenoise.evans.io/en/stable/changelog.html#v4-0

The WSGI integration option for Django (which involved editing wsgi.py) has been removed. Instead, you should add WhiteNoise to your middleware list in settings.py and remove any reference to WhiteNoise from wsgi.py. See the documentation for more details. (The pure WSGI integration is still available for non-Django apps.)

Pre v4.0

Heroku recommends this method at: https://devcenter.heroku.com/articles/django-assets:

Your application will now serve static assets directly from Gunicorn in production. This will be perfectly adequate for most applications, but top-tier applications may want to explore using a CDN with Django-Storages.

Install with:

pip install whitenoise
pip freeze > requirements.txt

wsgi.py:

import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "free_books.settings")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)

Tested on Django 1.9.




回答3:


The gunicorn should be used to serve the python "application" itself, while the static files are served by a static file server ( such as Nginx ).

There is a good guide here: http://honza.ca/2011/05/deploying-django-with-nginx-and-gunicorn

This is an excerpt from one of my configurations:

upstream app_server_djangoapp {
    server localhost:8000 fail_timeout=0;
}

server {
    listen < server port goes here >;
    server_name < server name goes here >;

    access_log  /var/log/nginx/guni-access.log;
    error_log  /var/log/nginx/guni-error.log info;

    keepalive_timeout 5;

    root < application root directory goes here >;

    location /static {    
        autoindex on;    
        alias < static folder directory goes here >;    
    }

    location /media {
       autoindex on;
       alias < user uploaded media file directory goes here >;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://app_server_djangoapp;
            break;
        }
    }
}

Some notes:

  • The static root, media root, static files path prefix and media file path prefix are set up in your settings.py
  • Once you have nginx set up to serve from the static content directory, you need to run "python manage.py collectstatic" in your project root so that the static files in the various apps can be copied to the static folder

In closing: while it is possible to serve static files from gunicorn ( by enabling a debug-only static file serving view ), that is considered bad practice in production.




回答4:


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.




回答5:


Since Django 1.3 there is django/conf/urls/static.py that handle static files in the DEBUG mode:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Read more https://docs.djangoproject.com/en/2.0/howto/static-files/#serving-static-files-during-development



来源:https://stackoverflow.com/questions/12800862/how-to-make-django-serve-static-files-with-gunicorn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!