Turn: http://blog.csdn.net/zyz511919766/article/details/18355827
Django will be in the configuration file setting by default. enable 'django. template. loaders. filesystem. loader '. After enabling this option, you can find and load the template according to the sequence of paths listed in template_dirs.
For example, the following configuration is available:
Template_loaders = (
'Django. template. loaders. filesystem. loader ',
)
Template_dirs = (
'/Var/www/site/mycitsm/templates ',
'/Var/www/site/mycitsm/sqlreview/templates ',
)
Now, the two Directories specified in template_dirsare stored in base.html, and the rendering template statement is return render (request, 'base.html', context}. then djangowill use the base.html template in the first directory first. When base.html does not exist in the first directory, djangowill use the base.html template in the second directory. Of course, when neither of the two directories has base.html, the system will prompt that the template cannot be found. To avoid confusion, try not to place templates of the same name in different locations specified by template_dirs when using 'django. template. loaders. filesystem. loader.
What if I want to place a template with the same name in different locations? For example, to achieve the purpose of program reuse, we often create static and template files specific to a Django app and store them in a specific directory of the app. However, we cannot ensure that these files do not have the same names as those in other locations. Therefore, another template Loading Mode 'django. template. loaders. app_directories.loader. After this option is enabled, you can find the template file to be rendered from the templates/directory of the installed app in installed_apps (for static files, the static/directory of the APP ).
For example, the following configuration is available:
Template_loaders = (
'Django. template. loaders. app_directories.loader ',
)
Template_dirs = ()
Here we didn't specify the path information that contains the template file in template_dirs, but because we use 'django. template. loaders. app_directories.loader 'Load method, which automatically searches for the corresponding template file from the templates directory of the app. For example, if the rendering statement is return render (request, 'base.html ', context), the template directory corresponding to the app is/var/www/site/mycitsm/sqlreview/templates/. Only base.html exists in the directory, django renders the template. If the template does not exist, the system prompts that the template file cannot be found. In addition, the template file will not be found from other places.
You may have thought of it carefully: What if two loading templates are used at the same time? For example, if you use 'django. template. loaders. filesystem. loader 'and 'django. template. loaders. app_directories.loader', how can we find and load the template?
For example, the following configuration is available:
Template_loaders = (
'Django. template. loaders. filesystem. loader ',
'Django. template. loaders. app_directories.loader ',
)
Template_dirs = (
'/Var/www/site/mycitsm/templates ',
'/Var/www/site/mycitsm/sqlreview/templates ',
)
The base.html template exists in the two Directories specified in template_dirs. the rendering template statement is return render (request, 'base.html ', context). Django first searches for and loads the template based on the template Loading Method listed in template_loaders, the method is the same as above. If the template file cannot be found, use the second method listed, and so on until it is found or not found. In this case, Django either cannot find the template or load the template that was first found. If a template file with the same name exists in multiple different paths, the final loaded template is related to the order of the listed loading methods and the order of the directories containing the templates. This is often ambiguous and can easily cause confusion.
Therefore, the templates of the app are usually stored in the templates directory of the app, instead of directly storing the template file in the templates directory of the app, in this directory, create a directory named after the app. For example, if the app name is sqlreview, the directory where the app template is stored is... /sqlreview/templates/sqlreview/, when specifying the template to be rendered, you can limit the template file through the previous directory of the template file to avoid confusion, this actually provides a namespace. For example, return render (request, 'sqlreview/base.html ', context) can be found in the/var/www/site/mycitsm/sqlreview/templates directory. In this way, you don't have to worry about whether the template loaded by Django is correct or not.
Static files such as CSS, JS, and IMG are loaded in the same way as template files. The above conclusions are also applicable. This is also the loading method when the template inherits Other templates.
Explore the order in which Django loads templates