include笔记:
- 有些模版代码是重复的。因此可以单独抽取出来,以后哪里需要用到,就直接使用
include
进来就可以了。 - 如果想要在
include
子模版的时候,传递一些参数,那么可以使用with xxx=xxx
的形式。示例代码如下:{% include 'header.html' with username='zhiliao' %}
代码:
需要创建一个头部header.html文件与一个脚部footer.html文件,因为这两个文件是在重复使用的。
html文件:
header.html
footer.html
index.html
这是中间内容{{ username }}
{% include 'footer.html' %}
company.html
这是公司的中间部分
{% include 'footer.html' %}
school.html
这是学校的中间部分
{% include 'footer.html' %}
views.py
from django.shortcuts import render
def index(request):
context = {
“username”:“中国”
}
return render(request,“index.html”,context=context)
def company(request):
return render(request,‘company.html’)
def school(request):
return render(request,‘school.html’)