Django solve uWSGI in the static file is missing

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 runserverthe 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 runservercase 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.pyconfiguration, use django.contrib.staticfiles, and make the following configurations.

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'),
)
Static file settings

Phenomenon, STATIC_URLfile location, in uWSGI under, can not access the browser, and in the runservercase 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.pythe same level staticdirectory.

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
Within a static application file

In addition, in INSTALLED_APPSthe 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_URLwhere 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_ROOTThis parameter, in runserverthe case of useless, because you know the location where Django.

In the integrated django.contrib.staticfilesafter ./manage.pymore than a sub-command.

./manage.py collectstatic

This sub-command, will put all STATICFILES_DIRSfiles in the directory are copied to STATIC_ROOTthe. Here is the /srv/django/staticdirectory.

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 /staticis uWSGI the URL of prefix, while the latter /srv/django/staticis 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:

  1. Use collectstaticsub-commands to collect and copy related static files.
  2. Start uWSGI , which contains static-mapconfiguration, or --static-mapparameters.

In addition, if you are using Nginx proxy, you can also consider the static files to it. Not described here.

Guess you like

Origin www.cnblogs.com/Neroi/p/12387402.html