1.视图中的变量在html中展示 A.在视图中定义youPorn变量以及info_data字典 def index(request): name_info = { ‘name‘:‘SanJin‘, ‘age‘: 21, ‘hobbie‘: "show", ‘job‘: "stripper" } #return render(request,‘index.html‘,{‘your‘:"gaoguodeng learn python"}) return render(request, ‘index.html‘, { ‘youPorn‘: "SanJin Is on the show now!", ‘info_data‘: name_info }) B. 在html文件中引用。双大括号里面带变量名即可,如: {{info_data}} {{youPorn}} 2.django 的if 判断 {% if k == "job" %} <li style="background-color: red"> {{ k }}::{{ v }}</li> {% else %} <li style="background-color: blue"> {{ k }}::{{ v }}</li> {% endif %} 3. django 的for 循环,也就是在{% python命令 %} 如: {% for k,v in info_data.items %} <li> {{ k }}::{{ v }}</li> {% endfor %} 4.django 应用模版 A.在你的html中引用你想引用的html文件。这是全部被引用(继承) {% extends ‘index.html‘ %} B.在你的html文件中引用你想应用的html文件,但是html文件中有部分内容不希望被引用或想被替换(部分引用或部分继承,和部分替换。) B1.在希望引用的html文件模版中,用下面内容把你不希望被引用或想被替换的内容扩起来。 {% block page-contont %} ... ... {这里面的就是你不想被引用的内容或想替换的内容} ... {% endblock %} B2.如果你想替换的上面内容,那就需要用下面命令 {% extends ‘index.html‘ %} {% block page-contont %} 这里编写你希望替换后的内容(如果不写内容,即替换为空的,也就是不引用上面这里部分内容) {% endblock %}
本文出自 “奋斗吧” 博客,请务必保留此出处http://lvnian.blog.51cto.com/7155281/1856434
原文:http://lvnian.blog.51cto.com/7155281/1856434