With uWSGI to run a single Django application, there is a static file is missing pit. This article describes how the pit.
(Django version of this paper in 1.4 above, because there are ADMIN_MEDIA_PREFIX 1.3 before this pit, this does not involve.)
problem
A Django application, in ./manage.py runserver
the case of normal operation. In uWSGI case of running the page style strange, static files can not be found.
In other words, no problem in the case of debugging, static production environment can not find the file.
(If in runserver
case of debugging environment is also a problem, it is often misconfigured. This article does not discuss the case of configuration errors.)
The following examples illustrate.
In the settings.py
configuration, use django.contrib.staticfiles
, and make the following configurations.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
BASE_DIR = os.path.dirname(os.path.realpath(__file__)) STATIC_ROOT = '/srv/django/static' STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), )
Phenomenon, STATIC_URL
file location, in uWSGI under, can not access the browser, and in the runserver
case is no problem.
the reason
A Django application, generally there are two types of static files. First, static files within the application, and second, Django comes.
According to example, static files within the application code in the front and settings.py
the same level static
directory.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), )
In addition, in INSTALLED_APPS
the configuration of django.contrib.admin
, then there will be another set of static files in Django set position.
For example, a root in Python 3.6 installed version of Django , ADMIN static files: /usr/local/lib/python3.6/site-packages/django/contrib/admin/static/admin/
.
Finally, STATIC_URL
where there will be two types of static files. /static/*
And /static/admin/*
.
Understand the principle, it is apparently the reason. uWSGI did not know what position static files.
solve
STATIC_ROOT
This parameter, in runserver
the case of useless, because you know the location where Django.
In the integrated django.contrib.staticfiles
after ./manage.py
more than a sub-command.
./manage.py collectstatic
This sub-command, will put all STATICFILES_DIRS
files in the directory are copied to STATIC_ROOT
the. Here is the /srv/django/static
directory.
uWSGI also supports static files, you can add parameters by executing:
uwsgi --static-map /static=/srv/django/static
Or, in the configuration file to add:
static-map = /static=/srv/django/static
=
The front /static
is uWSGI the URL of prefix, while the latter /srv/django/static
is the path to a static file. This path, usually using an absolute path, but also support relative paths.
So, the production environment deployment solutions altogether in two steps:
- Use
collectstatic
sub-commands to collect and copy related static files. - Start uWSGI , which contains
static-map
configuration, or--static-map
parameters.
In addition, if you are using Nginx proxy, you can also consider the static files to it. Not described here.