This is for Django Beginners
Template Lookup Mechanism(模版查找机制)
Templates Directories
Django looks for templates in the directories specified in the TEMPLATES
setting in file myproject/settings.py
. The default configuration typically includes(根据配置TEMPLATES
查找):
-
Any directories you specify in the
DIRS
option of the TEMPLATES setting. -
The templates directory within each installed app(if
APP_DIRS
is set to True).Example configuration:
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': ['templates'], # Add your templates directory here 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ]
Lookup Order
- Directories specified in
DIRS
.(首先查找配置在DIRS
中的目录) templates/
folder of each installed app, in the order the apps are registered inINSTALLED_APPS
. So it’s suggested to use formatapp_name/template_name.html
intemplates/
folder. (在各installed app的templates目录下查找,如果同名,按照app在INSTALLED_APPS中的顺序查找.因此, 在templates/
中建议使用app_name/template_name.html
)
If it finds multiple templates with the same name in different directories, it uses the first one it encounters based on the lookup order.(如果在不同目录下找到多个同名templates,返回找到的第一个)
Recommandation for Template Names
- For templates of app, it’s recommanded to place it under
project/app/templates/app/base.html
. - For templates of project, it’s recommanded to place it under
project/templates/base.html
Use the path relative to templates
in views:
# Render project template
return render(request, 'base.html')
# Render app template
return render(request, 'app/base.html')
Static File Lookup Mechanism(模版查找机制)
Static Files Directories
Django looks for static files in the directories specified in the STATIC_URL
and STATICFILES_DIRS
setting in file myproject/settings.py
. (根据配置STATIC_URL
和 STATICFILES_DIRS
查找)
-
Ensure
django.contrib.staticfiles
is configured inINSTALLED_APP
. -
STATIC_URL
: The URL prefix for accessing static files (e.g., static/). -
STATICFILES_DIRS
: A list of additional directories where Django will look for static files, beyond the default app-specific directories.Example configuration:
STATIC_URL = 'static/' STATICFILES_DIRS = [ BASE_DIR / "static", ]
Lookup Order
- Directories specified in
STATICFILES_DIRS
. static
directory within each installed app, in the order the apps are registered inINSTALLED_APPS
.(Django automatically includes these directories in the static file lookup)
If it finds multiple static files with the same name in different directories, it uses the first one it encounters based on the lookup order.(如果在不同目录下找到多个同名templates,返回找到的第一个).
Debug:
% python manage.py findstatic css/style.css
Found 'css/style.css' here:
myproject/static/css/style.css
myproject/myapp/static/css/style.css
myproject/myapp2/static/css/style.css
Recommandation for static file name
- For static files of app, it’s recommanded to place it under
project/app/static/app/css/style.css
orproject/app/static/app/js/main.js
. - For static files of project, it’s recommanded to place it under
project/static/css/style.css
orproject/static/js/main.js
Use the path relative to static
in templates:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello world </title>
<!-- static files in app -->
<link rel="stylesheet" href="{% static 'app/css/style.css' %}">
<script type='text/javascript' src="{% static 'app/js/main.js' %}"></script>
<!-- static files in project -->
<link rel="stylesheet" href="{% static 'css/style.css' %}">
<script type='text/javascript' src="{% static 'js/main.js' %}"></script>
</head>