template复用 extends block include render
参考:https://code.ziqiangxuetang.com/django/django-template.html
最基本的复用:
from django.shortcuts import render
return render(request,'hello.html',{'arg1':'val1'})
(1)撰写基本模板(所有可重定义的地方用{% block 块名 %}XXX{% endblock %}表示出来):
<!DOCTYPE html>
< html >
< head >
< title >{% block title %}默认标题{% endblock %} - 自强学堂</ title >
</ head >
< body >
{% include 'nav.html' %}
{% block content %}
<div>这里是默认内容,所有继承自这个模板的,如果不覆盖就显示这里的默认内容。</div>
{% endblock %}
{% include 'bottom.html' %}
{% include 'tongji.html' %}
</ body >
</ html >
(2)继承模板(开头处声明要继承的模板{% extends 'html文件名' %},重写处用{% block 块名 %}XXX{% endblock %}表示出来):
{% extends 'base.html' %}
{% block title %}欢迎光临首页{% endblock %}
{% block content %}
{% include 'ad.html' %}
这里是首页,欢迎光临
{% endblock %}
(3)调用模板{% include '模板名' %}:
{% include 'ad.html' %}