Solve the problem of static file when django has multiple APP

  • 2021-11-29 08:36:47
  • OfStack

Each APP has its own static folder, so how to set it will not be mixed

Here's an example (I have two APP in my project (login and main))

1. First modify the configuration path in the setting file


STATIC_URL = '/static/'
STATICFILES_DIRS = [
  os.path.join(BASE_DIR, "MGStudio", "static"),
  os.path.join(BASE_DIR, "main", "static"),
  os.path.join(BASE_DIR, "login", "static"),
]

2. Create a folder with the same name as APP under static under each APP

For example, I am in login/static/login/

Put in styles JS CSS and so on

3. How to invoke styles


{% static 'main/img/firefox-logo-small.jpg' %}

{% static 'login/img/name.png' %}

Supplementary knowledge: The static file problem of xadmin can not be loaded after Django project goes online (the relationship between several static setting items in settings of django)

Most of them are settings. py setup problems of static.

If you don't set up other app static here, you can't load it either

1. settings. py is set like this.


STATIC_URL = '/static/'
# Be careful not to project with you here static Folder name 1 Sample , Because this is used to store all the static files collected .
# If you set the same , Equal operation collectstatic Warn when .
STATIC_ROOT = os.path.join(BASE_DIR, 'static1')

#  Our static files are separated 3 Parts 
#  Here we set it to 3 Path 
STATICFILES_DIRS = [
  os.path.join(BASE_DIR, 'static'),
  os.path.join(BASE_DIR, 'myapp', 'static'),
  os.path.join(BASE_DIR, 'userapp', 'static'),
  ]

The first one above is static in the root directory of the project.

The following two are the other app's static, myapp, and userapp are the names of your other app

For example, if there is static under xadmin, it should be added

os. path. join (BASE_DIR, 'xadmin', 'static')

Set in urls (if there are more than one set in main urls)


from blog.settings import STATIC_ROOT

urlpatterns = [
  url(r'^admin/', admin.site.urls),
  
  #  Add access handlers for static files 
  url(r'^static/(?P<path>.*)/$', serve, {'document_root': STATIC_ROOT}),

2. Run in Project Directory

python manage.py collectstatic

At this time, all the static specified by you just set will be collected into 1 directory

3. Set the directory path of static to static1 just set in nginx


    charset utf-8;
    location / {
      include uwsgi_params;
      uwsgi_pass 127.0.0.1:8997;
      uwsgi_param UWSGI_SCRIPT blog.wsgi;
      uwsgi_param UWSGI_CHDIR /home/wwwblog/myblog/;
 
    }
    location /static/ {
    alias /home/wwwblog/myblog/static1/; # Static file directory 
    }

The following is related knowledge learning.

Relationship of Several static Setting Items in settings of django

The settings of django contains three static related settings:

STATIC_ROOT
STATIC_URL
STATICFILES_DIRS

STATIC_URL is easy to understand, that is, url mapped to static files, and 1 is generally /static/

STATICFILES_DIRS is a list of static directories for each app and the common static directory

STATIC_ROOT is the total static directory, and you can use commands to automatically collect static files

A more detailed explanation:

STATIC_ROOT: The directory to which static files will be copied after running manage. py collectstatic. Note: Do not put static files of your project in this directory. This directory will only be used when running collectstatic. I initially assumed that this directory and MEDIA_ROOT function is the same, so in the development environment 1 can not find static files.

STATIC_URL: Sets the start url of static file, which can only be referenced in template. This parameter has the same meaning as MEDIA_URL.

STATICFILES_DIRS: Static file locations that need to be managed in addition to the static directory of each app, such as static files common to the project. It has the same meaning as TEMPLATE_DIRS.

Each APP static/directory under the static file django development server will automatically find, this is similar to the previous APP templates directory.

Suppose there is a project djangodemo, and two app are demo1 and demo2

The django processes the static by merging the respective static of each app into one

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 up like this:


STATIC_URL = '/static/'
STATICFILES_DIRS = (
  os.path.join(BASE_DIR, 'static'),
 os.path.join(BASE_DIR, 'demo1', 'static'),
 os.path.join(BASE_DIR, 'demo2', 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static1')

Use commands

python manage.py collectstatic

All static files are automatically copied to STATIC_ROOT (that is, static1)

This step is necessary if admin or (xadmin) is turned on, otherwise the style file will not be found when deploying to the production environment


Related articles: