Django 2.1.7 public static configuration file, a common template path

13423234-c8e484b60d8cd3cc.png

In Django project, for static resource files js, css, images, etc., need a public storage path, or each application to create a new static folder, then there is very convenient.
So how to configure it?

Configure a static file path of settings.py

In STATIC_URL = '/static/'the following add code

13423234-1e35d24e381a466f.png
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

Under the new static project root folder and add the following sub-folders and files

File structure is as follows:

13423234-c43f1019f07b6be9.png

Okay, so that a good public static configuration files in the project. Let's continue configuring common template path.

Settings.py configuration template file path

TEMPLATES DIRS set of values ​​as follows:

'DIRS': [os.path.join(BASE_DIR, 'templates')],
13423234-c6c6166c07910ccc.png

Under the project path, create templates folder, and continue to create the application folder name

File structure is as follows:

13423234-6a847dc415acd850.png

So after such a configuration template path, application assetinfo how to call it?

View templates way calling application

13423234-a3d9d24fe9543c33.png

Because the definition of a good path templates, so you can just write the relative path under templates.

Here then put another front-end code, used to call the static test files.

Another set of front-end code into

13423234-c121d6a05bf629b0.png

You can know just configured the storage path static files, static html file path corresponding code also needs to be modified.

Increase in front of all the static files /static/path to, the following:

13423234-faaa0cb8c36a673c.png

Writing a view of the page display

13423234-775561490dbcea8a.png

Then configure the url path, use the following browsers to test and see, as follows:

13423234-111b2002c11b063e.png

You can see the page has been successfully demonstrated out.

View page source code, as follows:

13423234-0a8720918525b54c.png
13423234-d6245d0b99f41c57.png

We can see a static file is a normal visit.

13423234-0e3934319aa622f6.png

Reproduced in: https: //www.jianshu.com/p/e8c03c01dc6e

Guess you like

Origin blog.csdn.net/weixin_33912453/article/details/91263534