218👍
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
39👍
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.
- [Django]-Django override save for model only in some cases?
- [Django]-Separation of business logic and data access in django
- [Django]-CORS: Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true
26👍
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.
- [Django]-Django – How to rename a model field using South?
- [Django]-Django – filtering on foreign key properties
- [Django]-When to use Serializer's create() and ModelViewset's perform_create()
25👍
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 ).
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.
- [Django]-Django.contrib.gis.db.backends.postgis vs django.db.backends.postgresql_psycopg2
- [Django]-Passing variable urlname to url tag in django template
- [Django]-How to mix queryset results?
14👍
While you’re able to use Django to serve static files in development mode with the app django.contrib.staticfiles
, that is not suitable for production use.
In order to serve static files, as Jamie Hewland says, normally one routes all the requests to /static/ using Nginx
location /static/ {
alias /path/to/static/files;
}
Also, and as coreyward says about Gunicorn / Unicorn
was not designed to solve the suite of problems involved in serving
files to clients
Same line of reasoning applies if you consider other WSGI server like uWSGI instead of Gunicorn. In uWSGI documentation
it’s inefficient to serve static files via uWSGI. Instead, serve them
directly from Nginx and completely bypass uWSGI
So, serving static files in production is something to be done by NGINX, not by Django or other WSGI servers.
Another way is to serve your static files in production is using WhiteNoise library which is very easy to setup (you might want to use a CDN so that most requests won’t reach the Python app). As Miguel de Matos says, you just have to
-
Collect static
python manage.py collectstatic
-
Installing whitenoise
pip install whitenoise
-
Add the following
STATICFILES_STORAGE
in settings.pySTATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
-
Add the following to your
MIDDLEWARE
in settings.py (as mracette notes, "According to the whitenoise docs you should place the middleware afterdjango.middleware.security.SecurityMiddleware
")`MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', ... ]
- [Django]-URL-parameters and logic in Django class-based views (TemplateView)
- [Django]-Sending HTML email in django
- [Django]-Sending images using Http Post
2👍
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
- [Django]-Does SQLAlchemy have an equivalent of Django's get_or_create?
- [Django]-How do I get user IP address in Django?
- [Django]-New url format in Django 1.9
1👍
If you are using Apache/Gunicorn then here is how I set mine up.
-
In your Django root dir (with
manage.py
), create dirsmkdir -p django_static/static
-
In your project
settings.py
set the following:
DEBUG = False
INSTALLED_APPS = [..., 'django.contrib.staticfiles', ...]
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, "django_static", "static")
-
Run
python manage.py collectstatic
. This will output static content todjango_static/static
-
Start your
gunicorn
server withgunicorn your_project_name.wsgi
(plus options) -
Assuming you have default global Apache settings, you’ll need to create a soft link from
/var/www
to your static dir:
sudo ln -s /path/to/your_django_project/django_static /var/www/your_django_project_static
-
For your domain
www.example.com
that you wish to point to your Django app, configure the following virtual host in apache in order to proxy all requests submitted tohttps://www.example.com
onto127.0.0.1:8000
except forwww.example.com/static/
routes (in which case, serve files to such requests fromdjango_static
):
<VirtualHost *:443>
ServerName www.example.com
DocumentRoot /var/www/your_django_project_static
<Location "/">
ProxyPreserveHost On
ProxyPass http://127.0.0.1:8000/
ProxyPassReverse http://127.0.0.1:8000/
</Location>
<Location "/static/">
ProxyPass "!"
</Location>
</VirtualHost>
Voila!
- [Django]-Django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey"
- [Django]-Django fix Admin plural
- [Django]-Django: How can I create a multiple select form?