Through the template in Django, designers and Web site management have a docking interface to achieve web design and logical separation, so the template will handle a large number of text parsing content, Django internal use of efficient engine to complete the template parsing.
Template settings
Before using the template, Django needs to be configured, in the setting.py of the project directory,
TEMPLATES = [
? ? {
? ? ? ? ' Backend ': ' Django.template.backends.django.DjangoTemplates ',
? ? ? ? ' DIRS ': [
? ? ? ? ? ? '/vagrant/realhealth/myapp/template ',
? ? ? ? ],
? ? ? ? ' 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 ',
? ? ? ? ? ? ],
? ? ? ? },
? ? },
]
Where backend specifies the template parsing engine, and Django has two parsing engines built into it:? Django.template.backends.django.DjangoTemplates and? Django.template.backends.jinja2.Jinja2?;
DIRS the list of configuration template paths, the engine will look for the corresponding template from the path of the list;
App_dirs default to True to specify whether to look for templates from the app directory;
And a few other configurations.
Because the template loads the engine from the settings, the Python environment variable must be configured:
1. Add the project directory to the PYTHONPATH
2. Add Mysite.settings to? Django_settings_module, or when the template is used, add the os.environ[' django_settings_module ' = ' mysite.settings ', where MySite is your project name
Template
From the Django source code, you can see that the template structure is very simple, consisting of the templates string, template engine, rendering method three parts. The template string contains the HTML framework and some template languages, the engine splits the string into a template language, divides it into node, and uses some short regular expressions to handle the node:
Import OS
From django.template Import Template
os.environ[' django_settings_module '] = ' realhealth.settings '
def test ():
? ? t = Template ("' Hi, {{name}}}, it is {{time}}" now
{% if ordered%}
You are welcome!
{% Else%}
Sorry, you is not ordered
{% ENDIF%}
This is a very a long long long long string
‘‘‘)
? ? Print (t)
? ? For node in T:
? ? ? ? Print (node)
if __name__ = = ' __main__ ':
? ? Test ()
The resulting output is:
<django.template.base.template Object at 0x7f8cab56eb38>
<text Node: ' Hi, ' >
<variable node:name>
<text Node: ', it is ' >
<variable node:time>
<text Node: ' Now
' >
<text Node: '
You are welcome!
' >
<text Node: '
Sorry, you're not order ' >
<text Node: '
This is a very long long ' >
As you can see, the template string is split into many node, and the static string is truncated simply because it does not need to be processed.
Once you have created a template, you need to render it using a context object through the Render method.
Context
There is a dictionary-like structure in the context object that stores the mappings for the keywords and values needed in the template. A template can render different results from several different context objects. The implementation structure of the context is also very simple:
Can be seen, the context is mainly Current_app, Bind_template, __copy__, Update and other methods. Current_app and bind_template should be primarily related to the rendering implementation process. Because the context is a mapping mutable type similar to a dictionary, there is a __copy__ method to implement hard copies, and the Update method also prompts us that the contents of the context object can be updated.
Here is an example of a rendering:
Import OS
Import time
From django.template import Template,context
os.environ[' django_settings_module '] = ' realhealth.settings '
def test ():
? ? t = Template ("' Hi, {{name}}}, it is {{time}}" now
{% if ordered%}
You are welcome!
{% Else%}
Sorry, you is not ordered
{% ENDIF%}
This is a very a long long long long string
‘‘‘)
? ? Time_str = Time.strftime ('%y-%m-%d%x ', Time.localtime ())
? ? c = Context ({' name ': ' Lyon ', ' time ': time_str, ' ordered ': True})
? ? For I in C:
? ? ? ? Print (i)
? ? T_1 = T.render (c)
? ? Print (t_1)
? ? c = c.update ({' name ': ' Jake ', ' Ordered ': False})
? ? T_2 = T.render (c)
? ? Print (t_2)?
if __name__ = = ' __main__ ':
? ? Test ()
The output is:
{' name ': ' Lyon ', ' time ': ' 2015-06-20 23:07:33 ', ' Ordered ': True}
{' false ': false, ' none ': None, ' true ': true}
Hi, Lyon, it's 2015-06-20 23:07:33 now
Your is welcome!
This is a very a long long long long string
Hi, Jake, it's 2015-06-20 23:07:33 now
Sorry, you is not ordered
This is a very a long long long long string
The template in this output is separated by multiple line breaks, prompting us, perhaps, to pay attention to the line break in template.
Django Learning Diary 04_ Template _overview