您的位置:首页 > Web前端 > HTML

django 内容展示从views到html

2016-04-18 09:40 459 查看
nav.html, bottom.html, tongji.html

base.html
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}默认标题{% endblock %} - lanny教堂</title>
</head>
<body>
{% include 'nav.html' %}
//定义块,以后模版继承base后可以替换.
{% block content %}
<div>这里是默认内容,所有继承自这个模板的,如果不覆盖就显示这里的默认内容。</div>
{% endblock %}

{% include 'bottom.html' %}
{% include 'tongji.html' %}
</body>
</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.py
def 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 %}


本文出自 “LannyMa” 博客,请务必保留此出处http://lannyma.blog.51cto.com/4544390/1764899
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: