模板标签种类
{% for blog in blog_list %}
<h2>{{ blog.title }}</h2>
<p>{{ blog.timestamp }}</p>
<p>{{ blog.body }}</p>
{% endfor %}
#变量标签用{{}}来表示
#块级标签用{%%} 来表示
模板继承block
{% block content %}{%endblock%}这个部分,会被替换为集成页面{% extends "xx.html"%}{%endblock%}中级的内容。
content是个标签,也可以是title,css,js等。
原则是,一旦发现在页面中有大量重复内容需要复制黏贴,就应该使用模板。
{% block content %}
{%endblock%}
#在base.html基础模板文件里,在内容部分加上命名块标签
{% extends "blog/base.html"%}
{% block content %}
{% for blog in blog_list %}
<h2>{{ blog.title }}</h2>
<p>{{ blog.timestamp }}</p>
<p>{{ blog.body }}</p>
{% endfor %}
{%endblock%}
#在内容页面只要继承该模板,该页面就会使用模板风格
文件包含include
<!-- include left -->
{% include 'base/left_menu.html' %}
<!-- include heder -->
{% include 'base/head_menu.html' %}
<!--right content-->
<div class="right_col" role="main">
{% block content %}{% endblock %}
</div>
<!-- include footer -->
{% include 'base/foot_menu.html' %}#}
#在这段代码中,几个菜单都是分离出去的,这样提高了可读性
{% load static %}
<script src="{% static 'js/jquery.min.js' %}"></script>
<script src="{% static 'js/bootstrap.min.js' %}"></script>
<script src="{% static 'js/custom.min.js' %}"></script>
<script src="{% static 'js/icheck.min.js' %}"></script>
<script src="{% static 'js/sweetalert/sweetalert.min.js' %}"></script>
#在setting中设定static文件目录,然后在html文件头load static的话,下面可以直接用{% static xx%}来调用static文件。
#如果没有,就要用文件路径去调用static文件
{% url 'index' %}
<li><a href="{% url 'index' %}">控制台</a></li>
#在url中为url设置name属性后,可以直接调用name,而不需要使用url
{{ request.user }}
在网页中需要显示当前登录账户的用户名,可以直接用此渲染
列表嵌套字典for循环
<table border="1">
<thead>
<th >name</th>
<th>age</th>
<th>email</th>
</thead>
<tbody>
{% for dict in user_list %}
<tr>
<td>{{ dict.name }}</td>
<td>{{ dict.age }}</td>
<td>{{ dict.email }}</td>
</tr>
{% endfor %}
</tbody>
</table>
2 字典嵌套字典for循环
<table border="1">
<thead>
<th >name</th>
<th>tally</th>
</thead>
<tbody>
{% for name,tally in user_dict.items %}
<tr>
<td>{{ name }}</td>
<td>{{ tally }}</td>
</tr>
{% endfor %}
</tbody>
</table>