Python一日一練103----Django模版練習(xí)
來(lái)源:程序員人生 發(fā)布時(shí)間:2015-02-07 08:58:05 閱讀次數(shù):3270次
要求
request.META 是1個(gè)Python字典,包括了所有本次HTTP要求的Header信息,比如用戶IP地址和用戶Agent。通過(guò)request.META.items()可得到這個(gè)字典。要求將META信息輸出到網(wǎng)頁(yè)。
輸出結(jié)果

源碼
index.html
<html>
<head><title>{{index_title}}</title></head>
<body>
{% block mainbody %}
{%for key,value in index_text %}
<p><tr><td>{{key}}</td><td>{{value}}</td></tr></p>
{%endfor%}
{% endblock %}
</body>
</html>
views.py
from django.shortcuts import render
# Create your views here.
def display_meta(request):
values = request.META.items()
html = {}
html['index_title']='Display META'
html['index_text']=values
return render(request,"index.html",html)
毛病版本
index.html
<html>
<head><title>{{index_title}}</title></head>
<body>
{% block mainbody %}
{{index_text}}
{% endblock %}
</body>
</html>
views.py
from django.shortcuts import render
# Create your views here.
def display_meta(request):
values = request.META.items()
html = {}
html['index_text']=" "
html['index_title']='Display META'
for k, v in values:
html['index_text']+=('<tr><td>%s</td><td>%s</td></tr>' % (k, v))
return render(request,"index.html",html)
輸出結(jié)果

由圖可見(jiàn),閱讀器不能將模版標(biāo)簽的輸出信息進(jìn)行解釋。
源碼下載:http://download.csdn.net/detail/a359680405/8388657
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)