django url和返回内容到html
文章思维导图
nav.html, bottom.html, tongji.html
base.html
<!DOCTYPEhtml><html><head><title>{%blocktitle%}默认标题{%endblock%}-lanny教堂</title></head><body>{%include'nav.html'%}//定义块,以后模版继承base后可以替换.{%blockcontent%}<div>这里是默认内容,所有继承自这个模板的,如果不覆盖就显示这里的默认内容。</div>{%endblock%}{%include'bottom.html'%}{%include'tongji.html'%}</body></html>
home.html继承且覆盖base
{%extends'base.html'%}{%blocktitle%}欢迎光临首页{%endblock%}{%blockcontent%}{%include'ad.html'%}这里是首页,欢迎光临{%endblock%}
包含语法
{%include'bottom.html'%}{%blockcontent%}{%include'ad.html'%}{%endblock%}
字符串
views.pydefhome(request):string=u"我在lanny教堂学习Django,用它来建网站"returnrender(request,'home.html',{'string':string})home.html{{string}}
列表:
views.pydefhome(request):TutorialList=["HTML","CSS","jQuery","Python","Django"]returnrender(request,'home.html',{'TutorialList':TutorialList})在视图中我们传递了一个List到模板home.html:教程列表:{%foriinTutorialList%}{{i}}{%endfor%}
home.html
教程列表:
{%foriinTutorialList%}{{i}}{%endfor%}
字典
views.py
defhome(request):info_dict={'site':u'lanny教堂','content':u'各种IT技术教程'}returnrender(request,'home.html',{'info_dict':info_dict})home.html
法1:
站点:{{info_dict.site}}内容:{{info_dict.content}}法2:遍历{%forkey,valueininfo_dict.items%}{{key}}:{{value}}{%endfor%}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。