模板继承
模板继承和类的继承含义是一样的,主要是为了提高代码重用,减轻开发人员的工作量
{% extends 'base.html' %}
base.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{% block title %} {% endblock %}</title> {% block css %} {% endblock %} </head> <body> {% block content %} {% endblock %} {% block js %} {% endblock %} </body> </html>
index.html
{% extends 'base.html' %} {% block title %} 登录首页 {% endblock %} {% block css %} <style> p{ color:red } </style> {% endblock %} {% block content %} <P>姓名:<input type="text"></P> <P>密码:<input type="text"></P> {% endblock %} {% block js %} <script>alert('欢迎登录')</script> {% endblock %}
if 、ifequal语句
{% if today_is_weekend %} <p>Welcome to the weekend!</p> {% else %} <p>Get back to work.</p> {% endif %} {% ifequal user currentuser %} <h1>Welcome!</h1> {% endifequal %}
for循环
{% for athlete in athlete_list %} <li>{{ athlete.name }}</li> {{forloop.counter0}} #它是从0计数的 {{forloop.counter}} 它是从1计数的 {% if forloop.first %}<li class="first">{% else %}<li>{% endif %} {% if forloop.last %}<li class="last">{% else %}<li>{% endif %} {% endfor %}
注释标签
{% comment %} This is a multi‐line comment. {% endcomment %}
include标签
允许在模板中包含其它的模板的内容 {% include "nav.html" %} #使用关键字参数向模板传递额外的上下文 {% include "name_snippet.html" with person="Jane" greeting="Hello" %}