In the website development, must use the picture, the CSS file, the JS file and so on static file, with the Java EE in by the relative path lookup pattern dissimilarity, in Django, the static file loading, with the template similar, You need to set up in the settings.py file to tell the Django project where the static files are stored, which is the method used during the development process, where the static files are handled by the Ngnix server.
The steps given in the Django document
At this point, I will outline the main steps to facilitate beginners ' reference.
Settings in the 1.settings.py file
(1) Adding django.contrib.staticfiles to the Installed_apps
Installed_apps = (
' django.contrib.admin ',
' Django.contrib.auth ', '
django.contrib.contenttypes ',
' django.contrib.sessions ',
' django.contrib.messages ', '
django.contrib.staticfiles ', #安装此app
' Blog ',
)
(2) Set up Static_url and Staticfiles_dirs, which are the folders that represent static files in the root directory. Here, the folder I named is static
Static_url = '/static/'
staticfiles_dirs = (
os.path.join (Base_dir, "static")
2. Now put the static file in the above steps set in the specified path is OK
3. In the HTML file, omit the root directory, you can access to the corresponding static file.
This approach is only available for development use, which is inefficient and unsafe for actual deployment, and is actually deployed to refer to the actual deployment of static files