One of the three layers of Django’s MTV model, template basics (3)

Web design code modifications are frequent compared to Python, and Python code writing and HTML designers are two different tasks. Web designers should not be required to change Python code to meet front-end display requirements.

A template is a text used to separate the presentation and content of a document.
Template defines placeholders and various parts of the basic logic (template tags) for how various specification documents are displayed.
Templates usually generate HTML, but Django's templates can also generate documents in any text-based format.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>反馈信息</title>
</head>
<body>
<h1>面试反馈信</h1>
<p>亲爱的{
   
   {person_name}}:</p>
<ul>{% for item in items %}
    <li>{
   
   { item }}</li>
    {% endfor %}
</ul>
{% if yes %}
    <p>正确</p>
{% else %}
    <p>不正确</p>
{% endif %}
</body>
</html>

The content enclosed in two curly brackets is a variable (for example { {person_name}}), this means that the value of the specified variable can be inserted here; content surrounded by braces and percent signs (such as {% for item in items %}, { % if yes %}, etc.), is a template tag; the tag (tag) is relatively clearly defined, that is, it only notifies the template system to complete certain work tags.

{ {}} change
{% %} phrase

{% end~ %} phrase conclusion

Use of templates

(1) Template writing

The content is as shown in the appeal code

(2) Create template object

Create a template object to create a statement or variable in the template that accepts a variable value (for example { {}} or {%%})
Insert image description here

CreateContext

When we create the template object, we need to create the data that needs to be filled in ourselves.
What we need to pay attention to is render©, where c is the value (class) we want to pass in, but We want to pass it in as a dictionary.
Insert image description here

template rendering

For template rendering, data is passed in

Results displayPlease add image description

Label

1. if/else is a simple judgment statement
2. for is a simple iteration
3. ifequal ifnotequal compares two value, determine whether they are equal
4. Comment {# #}This is Djiango’s comment
5. Multi-line comment {% comment %}{% endcomment % }

Fasle:
Empty sequence table
Empty element group
Empty dictionary
Blank character skewer
Zero (0)
None
Fasle

filter

For filters, it is to filter out unnecessary data and display the data we need.

1. addslashes 添加反斜杠到任何反斜杠、单引号或者双引号前面。
2. date 按指定格式的字符串参数格式化日期对象。例如({ {pub_date|date:"Fj,Y"}})
3. length 返回变量的长度
4. upper 将字符串全部转化为大写格式
5. lower 将字符串全部转化成为小写格式
6. random 返回列表中的随机一项
7. add 给内部数值添加一个数值,例如({ {100|add:"100"}}),返回200
8. default 如果值不存在,则使用默认值代替
9. cut 删除指定字符串({ {“aaaaabbbbbccddddd”|cut:"cc"}}),返回‘aaaaabbbbbddddd’
10.capfirst 第一个字母大写,({ {“aa”|capfirst}}),返回Aa

That’s it for the basics of templates, I hope this is useful to you!
Thank you for your likes and comments!

Guess you like

Origin blog.csdn.net/qq_44936246/article/details/119889165