模板标签


def templ_tag(resquest):
""" 模板引擎标签 """
list_city = ('长沙', '广州', '深圳')
list_prods = [
{"name": "名称1", 'price': 100},
{"name": "名称2", 'price': 200},
{"name": "名称3", 'price': 300},
]
list_order = ['订单']
list_order2 = []

# 字典
user_info = {
'username': '张三',
'age': 35
}

return render(resquest, 'tag.html', {
'list_city': list_city,
'list_prods': list_prods,
'list_order': list_order,
'list_order2':list_order2,
'user_info': user_info
})



逻辑控制
and, or
==, !=
>, <
>=,<=
in, not in
is

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>标签使用</title>
<style type="text/css">
.row1{
background: red;
color:white;
}
.row2{
background: blue;
color:white;
}
</style>
</head>
<body>
{# 注释 #}
<!-- html注释 -->

<h5>城市渲染</h5>
<ul>
{% for item in list_city %}
<li> {{ item }} </li>
{% endfor %}
</ul>


<h5>商品渲染 </h5>
<ul>
{% for item in list_prods %}
{% if item.price >= 100 %}
<li>
{{ item.name }}: {{ item.price }}
</li>
{% endif %}
{% endfor %}
</ul>

<h5>商品渲染 倒序 reversed </h5>
<ul>
{% for item in list_prods reversed%}
{% if item.price >= 100 %}
<li>
{{ item.name }}: {{ item.price }}
</li>
{% endif %}
{% endfor %}
</ul>



<h5>商品渲染 下标</h5>
<ul>
{% for item in list_prods reversed%}
{% if item.price >= 100 %}
<li>

{# 0开头#}
{{ forloop.counter0 }}:

{# 1开头#}
{# {{ forloop.counter }}:#}
{{ item.name }}=> {{ item.price }}
</li>
{% endif %}
{% endfor %}
</ul>


<h5>商品渲染 隔行换色</h5>
<ul>
{% for item in list_prods reversed%}
{% if item.price >= 100 %}
<li class="{% cycle 'row1' 'row2' %}">
{{ forloop.counter0 }}:
{{ item.name }}=> {{ item.price }}
</li>
{% endif %}
{% endfor %}
</ul>





<h5>订单渲染</h5>

{% for item in list_order %}
<li>{{ item }}</li>
{% endfor %}


<h5>订单渲染2</h5>
{% for item in list_order2 %}
<li>{{ item }}</li>
{% empty %}
<li>暂无订单</li>
{% endfor %}


<h5>渲染对象</h5>

{% for key, value in user_info.items %}
<p>
{{ key }}: {{ value }}
</p>
{% endfor %}
{# username: 张三 #}
{# age: 35 #}


<h5>url标签的使用</h5>

<a href="{% url 'print_resp_attr' %}" target="_blank">
打印响应对象
</a>

{#<h5>带参数的url</h5>#}

{#<a href="{% url 'print_resp_attr' 2020 %}?month = 12" target="_blank">#}
{# 2019d 文章#}
{#</a>#}


{#<h5>带命名空间的url</h5>#}

{#<a href="{% url 'author:show_class' %}" target="_blank">#}
{# 带命名空间的url #}
{#</a>#}


</body>
</html>
posted @ 2019-09-02 13:47  EricBlog  阅读(147)  评论(0编辑  收藏  举报