Home > Backend Development > Python Tutorial > Django Basics Tutorial - Templates

Django Basics Tutorial - Templates

高洛峰
Release: 2016-10-17 14:16:16
Original
1110 people have browsed it

Note: The python version is 3.3.1, the Django version is 1.5.1, and the operating system is Windows 7. There are some differences in other versions. Readers can explore by themselves.

You may have discovered this problem in the previous chapter, that is, when the view returns text, the HTML code is hard-coded in the python code. Such as %s and so on. Writing like this often makes the program more complicated, and it becomes very troublesome once modified. Moreover, HTML code programmers may not necessarily know Python code. Current development generally separates the HTML front-end page and the Python back-end, that is, the front-end is only responsible for displaying the page. , the background is only responsible for processing data and other operations. Therefore, templates are particularly important.

So, what is a template?

A template is a text used to separate the presentation and content of a document. Templates define placeholders and various pieces of basic logic (template tags) that specify how the document should be displayed. Templates are typically used to generate HTML, but Django's templates can also generate documents in any text-based format. Let's learn what a template is from a simple example. (This example comes from DjangoBook2)

 <head><title>Ordering notice</title></head>
 <body>
 <h1>Ordering notice</h1>
 <p>Dear {{ person_name }},</p>
 <p>Thanks for placing an order from {{ company }}. It&#39;s scheduled to
 ship on {{ ship_date|date:"F j, Y" }}.</p>
 <p>Here are the items you&#39;ve ordered:</p>
 <ul>
 {% for item in item_list %}
     <li>{{ item }}</li>
 {% endfor %}
 </ul>
 {% if ordered_warranty %}
     <p>Your warranty information will be included in the packaging.</p>
 {% else %}
     <p>You didn&#39;t order a warranty, so you&#39;re on your own when
     the products inevitably stop working.</p>
 {% endif %}
 <p>Sincerely,<br />{{ company }}</p>
 </body>
 </html>
Copy after login


As shown above, the way to replace python code with {{...}} or {%...%} is template, Like the first {{person_name}} is actually a variable, and {%for....%} or {% if...%} are loops. Without delving into the meaning of the above code, let's learn how to use it step by step.

>>> from django import template
>>> t = template.Template(&#39;My name is {{ name }}.&#39;)
>>> c = template.Context({&#39;name&#39;: &#39;Adrian&#39;})
>>> print(t.render(c))
My name is Adrian.
>>> c = template.Context({&#39;name&#39;: &#39;Fred&#39;})
>>> print(t.render(c))
My name is Fred.
Copy after login


When you see the above code, you may be impatient to try it, but an error occurs on the second line. Generally speaking, the only error that may occur is: 'DJANGO_SETTINGS_MODULE' error. This is because when Django searches for the DJANGO_SETTINGS_MODULE environment variable, it is set in settings.py, and starting the python shell directly will cause it to not know which configuration file to use. For example, assuming mysite is in your Python search path, then DJANGO_SETTINGS_MODULE should be set to: 'mysite.settings'. So in order to avoid the trouble of setting environment variables, we should start the python shell like this.

python manage.py shell

This will save you the trouble of configuring environment variables that you are not familiar with.

Let’s analyze that piece of code.

>>> from django import template  #从django中导入template对象
>>> t = template.Template(&#39;My name is {{ name }}.&#39;)  #使用template对象的Template()方法
>>> c = template.Context({&#39;name&#39;: &#39;Adrian&#39;})  #使用template对象的Context()函数给赋值,比如name的值就是Adrian,Context()的()里面是一个字典
>>> print(t.render(c))   #渲染模板,也就是讲Context赋值后的name的值Adrian替换上面Template()中的{{name}}并输出
My name is Adrian.
>>> c = template.Context({&#39;name&#39;: &#39;Fred&#39;})
>>> print(t.render(c))
My name is Fred.
Copy after login


As you can see from the above example, there are three steps to using templates. 1. Call the Template function; 2. Call the Context function; 3. Call the render function. It's that simple.


Let’s talk about the Context() function through a few codes.

#代码段1:
>>> from django.template import Template,Context
>>> t=Template(&#39;hello,{{name}}&#39;)
>>> for name in (&#39;A&#39;,&#39;B&#39;,&#39;C&#39;):
...     print(t.render(Context({&#39;name&#39;:name})))
...
hello,A
hello,B
hello,C
#代码段2:
>>> from django.template import Template,Context
>>> person={&#39;name&#39;:&#39;Thunder&#39;,&#39;age&#39;:&#39;108&#39;}
>>> t=Template(&#39;{{person.name}} is {{person.age}} years old!&#39;)
>>> c=Context({&#39;person&#39;:person})#后面的这个person是一个字典
>>> t.render(c)
&#39;Thunder is 108 years old!&#39;
#代码段3:
>>> from django.template import Template,Context
>>> t=Template(&#39;Item 2 is {{items.2}}&#39;)#items.2的意思是调用items列表的第3个元素,因为列表的索引是从0开始的
>>> c=Context({&#39;items&#39;:[&#39;Apple&#39;,&#39;Banana&#39;,&#39;Orange&#39;]})
>>> t.render(c)
&#39;Item 2 is Orange&#39;
Copy after login


Note: items.2 above cannot be items.-1 or any other negative index.

Take a good look at the three pieces of code above. Are you just drawing inferences? Also by default, if a variable does not exist, the template system will display it as an empty string and do nothing to indicate failure.



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template