Each APP has its own static folder, so how to set it up and not mix it
The following is an example (I have two APPs (login and main) in my project)
1. First modify the configuration path in the setting file
STATIC_URL = '/static/' STATICFILES_DIRS = [ (BASE_DIR, "MGStudio", "static"), (BASE_DIR, "main", "static"), (BASE_DIR, "login", "static"), ]
2. Create a folder with the same name as the APP under static under each APP
For example, I'm login/static/login/
Put in style JS CSS, etc.
3. How to call styles
{% static 'main/img/' %} {% static 'login/img/' %}
Supplementary knowledge:The problem of not being able to load static files such as xadmin after Django project is launched (the relationship between several static settings in django settings)
Most of them are static settings issues.
If you don't set the static of other apps, you won't be able to load it.
This is set inside.
STATIC_URL = '/static/' #Be careful not to be the same as your project static folder name, because this is used to store all static files collected.#If you set the same, it will be warned when running collectstatic.STATIC_ROOT = (BASE_DIR, 'static1') # Our static files are separated by three parts# Here we set it to three pathsSTATICFILES_DIRS = [ (BASE_DIR, 'static'), (BASE_DIR, 'myapp', 'static'), (BASE_DIR, 'userapp', 'static'), ]
The first one above is static in the project root directory.
The following two are the static, myapp, and userapp of other apps, respectively.
For example, if there is static under xadmin, you should add it
(BASE_DIR, ‘xadmin', ‘static')
Set it in urls (if there are multiple settings in the main urls)
from import STATIC_ROOT urlpatterns = [ url(r'^admin/', ), # Add access processing function for static files url(r'^static/(?P<path>.*)/$', serve, {'document_root': STATIC_ROOT}),
2. Run in the project directory
python collectstatic
At this time, all the static you just set up to collect it into a directory
3. Set the static directory path in nginx to the static1 just set
charset utf-8; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8997; uwsgi_param UWSGI_SCRIPT ; uwsgi_param UWSGI_CHDIR /home/wwwblog/myblog/; } location /static/ { alias /home/wwwblog/myblog/static1/; #static file directory }
The following is the relevant knowledge learning.
Relationship between several static settings in django settings
Django's settings contain three static related settings:
STATIC_ROOT
STATIC_URL
STATICFILES_DIRS
STATIC_URL is easy to understand, it is a url mapped to a static file, generally /static/
STATICFILES_DIRS is a list that places the static directory of each app and the public static directory.
STATIC_ROOT is the total static directory, and static files can be collected automatically using commands.
More detailed explanation:
STATIC_ROOT: The directory to which the static file will be copied after running collectstatic. Note: Do not put the static files of your project into this directory. This directory is only used when running collectstatic. At first I took it for granted that the role of this directory and MEDIA_ROOT is the same, which makes it impossible to find static files in the development environment.
STATIC_URL: The starting url of the static file set, this can only be referenced in template. This parameter has similar meanings to MEDIA_URL.
STATICFILES_DIRS: In addition to the static directories of each app, the static file locations that need to be managed, such as the static files that are common to the project. It has similar meanings to TEMPLATE_DIRS.
The static file django development server under the static/ directory under each APP will be automatically found, which is similar to the templates directory under the previous APP.
Suppose there is a project djangodemo, and there are two apps: demo1 and demo2
Django's method of processing static is to merge the respective statics of each app into one place
for example
djangodemo/djangodemo/static Place public static files
djangodemo/demo1/static place the app's own static file
djangodemo/demo2/static place the app's own static file
You can set it like this:
STATIC_URL = '/static/' STATICFILES_DIRS = ( (BASE_DIR, 'static'), (BASE_DIR, 'demo1', 'static'), (BASE_DIR, 'demo2', 'static'), ) STATIC_ROOT = (BASE_DIR, 'static1')
Use commands
python collectstatic
All static files will be automatically copied to STATIC_ROOT (ie static1)
If admin or (xadmin) is enabled, this step is very necessary, otherwise the style file will not be found when deployed to the production environment
The above article solves the problem of static files in multiple django APPs is all the content I share with you. I hope you can give you a reference and I hope you can support me more.