Note: The python version is 3.3.1, the Django version is 1.5.1, the operating system is WINDOWS7, the other versions have some different local readers to explore on their own.
In the previous chapter you may have found that the HTML code is hardcoded in Python code when the view returns text. such as%s and so on. Writing like this often makes the program more complex, once the changes are very troublesome, and the HTML code programmer does not necessarily have Python code, now the development of the general will make HTML foreground page and Python background separation, that is, the foreground is only responsible for displaying the page, The background is only responsible for handling data and other operations. Therefore, templates are especially important.
So, what is a template?
A template is a text that separates the representation and content of a document. The template defines the placeholder and various basic logic (template tags) that are used to standardize how the document should be displayed. Templates are often used to generate HTML, but Django templates can also produce any document based on text format. Let's learn from a simple example of what a template is. (This example originates from DJANGOBOOK2)
Ordering Notice Ordering Notice
Dear {{Person_name}},
Thanks for placing a order from {{company}}. It's scheduled to ship on {{ship_date|date: "F J, Y"}}.
Here is the items you ' ve ordered:
{% for item in item_list%}
- {{Item}}
{% endfor%}
{% if Ordered_warranty%}
Your Warranty information'll be a included in the packaging.
{% Else%}
You didn ' t order a warranty, so you ' re on your own when the products inevitably stop working.
Sincerely,
{{company}}
Use {{...}} as shown above or {%...%} to replace the Python code is the template, like the first {{person_name}} is actually a variable, and {%for....%} or {% If ...%} and so on is the loop. Let's not go into the meaning of the above code, we take a step below to learn how to use it.
>>> from django import template>>> t = template. Template (' My name is {{name}}}. ') >>> c = Template. Context ({' name ': ' Adrian '}) >>> print (T.render (c)) My name is adrian.>>> c = template. Context ({' name ': ' Fred '} ') >>> print (T.render (c)) My name is Fred.
When you see the above code, you may be impatient to try it, and the result is wrong in the second line. In general, the only possible error is ' Django_settings_module ' because the DJANGO search Django_settings_module environment variables, It is set in settings.py, and the direct start of the Python shell causes it to not know which configuration file to use. For example, suppose MySite is in your Python search path, then django_settings_module should be set to: ' Mysite.settings '. So to avoid the hassle of setting environment variables, we should start the Python shell like this.
Python manage.py Shell
This frees you from the hassle of configuring environment variables that you don't know well.
Let's analyze the code below.
>>> from Django Import template #从django中导入template对象 >>> t = template. Template (' My name is {{name}}}. ') #使用template对象的Template () method >>> c = template. The Context ({' name ': ' Adrian '}) #使用template对象的Context () function assigns a value, such as the value of name Adrian,context () is a dictionary >>> Print (T.render (c)) #渲染模板, that is, the value of the name after the context assignment Adrian replace {{name}} in the above template () and output my name is adrian.>>> c = Template. Context ({' name ': ' Fred '} ') >>> print (T.render (c)) My name is Fred.
As you can see from the example above, three steps to using a template. Call the template function, second, call the context function, and call the render function. It's so simple.
Let's say the context () function in a few more code.
#代码段1:>>> from django.template import template,context>>> t=template (' Hello,{{name}} ') >> > for name in (' A ', ' B ', ' C '): ... Print (T.render (Context ({' name ': name})) ... hello,ahello,bhello,c# code snippet 2:>>> from django.template Import template,context>>> person={' name ': ' Thunder ', ' age ': ' 108 '}>>> t=template (' {{person.name}} ' is {{ Person.age}} years old! ') >>> C=context ({' Person ':p Erson}) #后面的这个person是一个字典 >>> T.render (c) ' Thunder are 108 years old! ' #代码段3:>>> from django.template import template,context>>> t=template (' Item 2 is {{items.2}} ') # Items.2 means that the 3rd element of the items list is called, because the index of the list is starting from 0 >>> c=context ({' Items ': [' Apple ', ' Banana ', ' Orange '}) >> > T.render (c) ' Item 2 is Orange '
Note: The above items.2 cannot be items.-1 or any other negative index.
Look at the above three pieces of code, is not extrapolate it? In addition, by default, if a variable does not exist, the template system will show it as an empty string without doing anything to indicate failure.