A site usually needs to save extra files, compared to slice CSS style files js script files, in Django, tend to refer to these files as static files. Django provides a django.contrib.staticfiles module to help us manage static files conveniently.
There are two ways to configure static files:
1 Configure a static file under a separate app, such as a separate picture under an app.
2 Configure static files under entire project for files that are not associated with individual apps, such as jquery bootstrap, etc.
Configuration steps:
1 First, we need to confirm the existence of the Installed_apps variable in the settings.py file Django.contrib.staticfiles
1Installed_apps = (2 'Django.contrib.admin',3 'Django.contrib.auth',4 'Django.contrib.contenttypes',5 'django.contrib.sessions',6 'django.contrib.messages',7 ' Django.contrib.staticfiles ',8)
2.1 Perform this step if we want to configure a static file under a separate app.
Define the Static_url variable in the settings.py file.
1 ' /static/ '
2.2 Perform this step if we want to configure static files under the entire project.
Defining Staticfiles_dirs variables in the settings.py file
1 staticfiles_dirs = (2 "static"), # Preferred project static file search path 3 '/var/www/static/', # The second selection of the project static file search path, you can also have a third choice, the fourth choice ... 4 )
3.1 Perform this step if we want to configure a static file under a separate app.
At this point, we need to create a folder named Static in the app.
3.2 Perform this step if we want to configure static files under the entire project.
We need to create a folder named static throughout project
4 When we are in the template either using the hard link /static/myexample.jpg or using the static label will first access the Myexample.jpg file under the static/folder under the app, and if the file does not appear in the static folder under the app, access the static/folder in project. Check if there is a myexample.jpg file, if any, then return, if not then go to/var/www/static/to find.
Static file Management in Django