Django study notes 3 - static file call

1. Settings.py static file related sample code and description:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
 
STATIC_URL = '/static/'
 
# When running python manage.py collectstatic
# The STATIC_ROOT folder is used to copy files in all folders in all STATICFILES_DIRS and files in static in each app
# Putting these files together is more convenient when deploying with apache, etc.
STATIC_ROOT = os.path.join(BASE_DIR, 'collected_static')
 
# Other folders for storing static files can be used to store public static files in the project, which cannot contain STATIC_ROOT
# If you don't want to use STATICFILES_DIRS, you can use it, you can also put it in static in the app
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, " common_static " ),
     ' /path/to/others/static/ ' , # You can omit this line when you don't need it
)
 
# This is the default setting, Django will find files in the folder in STATICFILES_DIRS and the static folder under each app by default
# Note that there is a sequence, if you find it, you will not continue to search
STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder"
)

2.STATIC_ROOT

STATIC_ROOT = os.path.join(BASE_DIR, ' collect_static ' )#BASE_DIR is the absolute address of the item

 

STATIC_ROOT is the directory of all static static aggregations when deploying static files (pyhton manage.py collectstatic),

STATIC_ROOT should be written as an absolute address, here, for example, my project mysite is /home/mysite/, then STATIC_ROOT is /home/mysite/collect_static/

When deploying the project, enter in the terminal:

python manage.py collectstatic

django will copy all static files to the STATIC_ROOT folder

3.STATICFILES_DIRS

STATIC_ROOT only comes into play during deployment, but in practice, there are two general placement locations for static files:

1. One is to create a new static folder in each app, put the static files in it, and load them When using static files ,
such as using static files in templates,

django will automatically search for the static folder in each app (so, don't spell the name of the folder wrong, otherwise django won't be able to find your folder


2. The other is to create a public folder outside all app files, because some static files are not unique to an app, so you can put them in

Go to a public folder for easy management (note that creating a public static file folder is just an easy-to-manage approach, but it is not necessary,

The app can apply static files across apps, because in the end all static files will exist in STATIC_ROOT) The question now is how to make

Django knows that you put some static files in public folders other than the app, then you need to configure STATICFILES_DIRS


STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'common_static'),
)

STATICFILES_DIRS tells django to first look for static files in STATICFILES_DIRS, and then look in the static folder of each app (note that django is lazy to find static files , find the first one, and stop searching)

 

3.STATIC_URL

So far, the mechanism of static files can work, but there is a question, can I directly access my static files in the project through the url, the answer is definitely yes, but, pay attention, you are in the browser is Access, you can't enter the local absolute address of your static file. For example, the local address of one of my pictures is /home/mysite/common_static/myapp/photo.png
, then it is impossible for others to enter it directly on the browser:
http : //192.168.1.2:8000/home/mysite/common_static/myapp/photo.png In
this way, the browser will report an error. Without this page, how
does django allow the browser to access the static files on the server? Having said that, it is not possible to directly access the local address of the server, then a mapping is required. django uses STATIC_URL to allow the browser to directly access static files, for example:

STATIC_URL = '/static/'

Then you can enter on the browser:
http : //192.168.1.2:8000/static/common_static/myapp/photo.png
Then it is equivalent to accessing /home/mysite/common_static/myap/photo.png

So on the browser, use the specific content of the prefix STATIC_URL to map STATIC_ROOT,
HTTP: //192.168.1.2:8000/static Equivalent to STATIC_ROOT of the local address

 

 

 Reference: https://blog.csdn.net/jj546630576/article/details/78606531

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325646529&siteId=291194637