文章思维导图
nav.html, bottom.html, tongji.html
base.htmlhtml>
{% block title %}默认标题{% endblock %} - lanny教堂{% include 'nav.html' %}
//定义块,以后模版继承base后可以替换.
{% block content %}
{% endblock %}
{% include 'bottom.html' %}
{% include 'tongji.html' %}
home.html继承且覆盖base{% extends 'base.html' %}
{% block title %}欢迎光临首页{% endblock %}
{% block content %}
{% include 'ad.html' %}
这里是首页,欢迎光临
{% endblock %}
包含语法{% include 'bottom.html' %}
{% block content %}
{% include 'ad.html' %}
{% endblock %}
字符串views.py
def home(request):
string = u"我在lanny教堂学习Django,用它来建网站"
return render(request, 'home.html', {'string': string})
home.html
{{ string }}
列表:views.py
def home(request):
TutorialList = ["HTML", "CSS", "jQuery", "Python", "Django"]
return render(request, 'home.html', {'TutorialList': TutorialList})
在视图中我们传递了一个List到模板 home.html:
教程列表:
{% for i in TutorialList %}
{{ i }}
{% endfor %}
home.html
教程列表:{% for i in TutorialList %}
{{ i }}
{% endfor %}
字典
views.pydef home(request):
info_dict = {'site': u'lanny教堂', 'content': u'各种IT技术教程'}
return render(request, 'home.html', {'info_dict': info_dict})
home.html
法1:站点:{{ info_dict.site }} 内容:{{ info_dict.content }}
法2:遍历
{% for key, value in info_dict.items %}
{{ key }}: {{ value }}
{% endfor %}