1. template Composition
The dynamic part of the template consists of tags ({% if %}) and variables {var }.
Context is used to transmit data to the template. It is a ing data similar to name-> value in the dictionary.
2. requestcontext
Why use requestcontext?
Code to answer:
Example 1:
From Django. template import loader, Context
Def view_1 (request ):
....
T = loader.get_template('template1.html ')
C = context ({
'App': 'My app ',
'User': request. user,
'IP _ address': request. Meta ['remote _ add'],
'Message': 'I am view 1 .'
})
Return T. Render (c)
Def view_2 (request ):
#...
T = loader.get_template('template2.html ')
C = context ({
'App': 'My app ',
'User': request. User
'IP _ address': request. Meta ['remote _ add'],
'Message': 'I am the second view .'
})
Return T. Render (c)
It can be seen that if there are multiple similar C, you need to re-write each time.
Requestcontext can help you solve this problem. The modified code is as follows:
Def comm_proc (request ):
Return {
'App': 'My app ',
'User': request. user,
'IP _ address': request. Meta ['remote _ add'],
'Message': 'I am view SDF .'
}
Def view_1 (request ):
Return render_to_response ("exercise/template.html", requestcontext (request, processors = [comm_proc])
3. Benefits of requestcontext
The default processor defined in Django. conf. global_setting.py
See the template_context_processors variable.
The default settings include:
Template_context_processors = (
'Django. Core. context_processors.auth ',
'Django. Core. context_processors.debug ',
'Django. Core. context_processors.i18n ',
'Django. Core. context_processors.media ',
)
You can set the processor public for all requestcontext on this site in setting.
Template_context_processors
If template_context_processors is defined in setting, the variable in Django. conf. global_setting.py becomes invalid.
Note: The processor defined in template_context_processors is used in each template,
Therefore, if you want to use a template to render requestcontext, note that the variable names must be unique (case sensitive)
Context_processor is recommended to place the file name in context_processors.py.
4. Automatic HTML escaping
The Django template automatically removes some key symbols, especially <,>, '', and.
Therefore, the Django template does not need to be specially processed.
However, the problem is that some trusted HTML will also be avoided, such as when an email is sent
To be avoided. The solution is as follows:
1) Avoid using a safe filter for a single variable
This will not be escaped: {data | safe }}
If the data value is <SCRIPT> alter ("hello") </SCRIPT>, and the safe filter is added
The dialog box is displayed. If this parameter is not added, the Django template will drop escape.
2) block comments
{% Autoescape off %}
Hello {name }}
{% Endautoescape %}
Autoscape off and autoscape on can be nested here.
5. Inside template Loading
Generally, you can use template_dir to specify the location where the template is stored in the operating system (which can be stored in any location in the system ),
In addition, you can customize the template loader to load the template,
Two load template methods:
1) Django. template. loader. get_template (template_name)
2) Django. template. loader. select_template (template_name_list ):
Select_template is just like get_template, doesn't it takes a list of template names.
Of the list, it returns the first template that exists. If none of the templates exist,
Three loaders (template_loaders): The loaders are prioritized in the order of template_laoders.
1) Django. template. loaders. filesystem. load_template_source
The loader loads the template from the file system, that is, the part defined in template_dirs.
2) Django. template. loaders. app_directories.load_template_source
From the name of the loader, we can see that the loader can automatically search for the template according to app_directories,
This loader is very convenient. As long as you have registered in the installed app (installed_app,
The loader will find whether there is a templates directory under the corresponding app directory when it is started for the first time. If so, the template under this directory will be loaded.
For example, if installed_apps contains ('myproject. polls',), then get_template('foo.html ')
Will look for templates:
/Path/to/myproject/polls/templates/foo.html
3) Django. template. loaders. Eggs. load_template_source
This loader loads templates from python eggs, which is disabled by default. If the app needs to be opened when it is published through eggs.
6. Creating a template library
Django allows users to create their own template libraries, including creating tags and filters,
You can also create your own loaders. These functions are very powerful and need to be carefully studied later.
The same is true of the Django framework template. For details, see.
Django/template/defaultfilters. py and Django/template/defaulttags. py
<End of this section>