模板繼承
- 多個(gè)頁(yè)面共用的部分單獨(dú)拎出來(lái)璃弄。這部分一般是導(dǎo)航欄跟頁(yè)腳磷仰。
nav.html
<div class="pusher">
<div class="ui menu">
<div class="header item" id="menu"> Menu
<i class="content icon"></i>
</div>
<div class="item">About us
</div>
<div class="item">Location</div>
<div class="item">Others</div>
</div>
{% block content %}
{% endblock %}
- nav.html 中引入了名為‘content’的block姆蘸。在content.html中聲明extends為nav.html暴氏,并定義名為content的block祝蝠。
{% extends 'nav.html' %}
{% block content %}
<div>
...
</div>
{% endblock %}
- views.py中定義一個(gè)新的view: contentview
contentview返回渲染后的content.html頁(yè)面音诈。
def contentview(request):
...
return render(request, 'content.html', context)
- urls.py中添加一個(gè)url。
指示訪問(wèn)index時(shí)绎狭,調(diào)用contentview细溅。
url(r'^index/', contentview, name='stainfo')
完成效果
Screen Shot 2016-07-24 at 4.14.00 PM.png
Screen Shot 2016-07-24 at 4.13.56 PM.png
為sidebar中的菜單項(xiàng)添加鏈接
Naming URL patterns
Name a URL and use it in template.
In urls.py
url(r'^statistic/', statisticdata, name='stainfo'),
In nav.html
{% url 'stainfo' %}
Done!