码迷,mamicode.com
首页 > 其他好文 > 详细

Django学习(3)模板定制

时间:2017-12-11 14:28:54      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:dea   文件夹   rom   margin   oge   min   学习   cin   视图   

  在Django学习(一)一首情诗中,views.py中HTML被直接硬编码在代码之中,虽然这样便于解释视图是如何工作的,但直接将HTML硬编码到视图却不算一个好主意。因为:

  • 对页面设计进行的任何改变都必须对Python代码进行相应的修改,而站点设计的修改往往比底层Python代码的修改要频繁得多。
  • Python代码编写和HTML设计是两项不同的工作,大多数专业的网站开发环境都将它们分配给不同的人员来完成。
  •  程序员编写Python代码和设计人员制作模板两项工作同时进行的效率是最高的,远胜于让一个人等待另一个人完成对某个既包含Python又包含HTML的文件的编辑工作。

  基于以上原因,Django推荐使用模板。模板(Template)是一个文本,用于分离文档的表现形式和内容。模板通常用于产生HTML.
  本次分享的目标是利用Django的模板来产生一封简单的情书。这需要用到Django模板方面的知识。
  先新建项目love_letter:

django-admin.py startproject love_letter

切换到该文件夹下的love_letter目录,新建letter.html文件,即Django的模板,代码如下:

 1 <html>
 2 <head>
 3 <title>Love Letter</title>
 4 </head>
 5 <body>
 6 <h1>Love Letter for {{ name }}</h1>
 7 
 8 <p>Dear {{ name }}:</p>
 9 
10 <p>Now you are reading a letter from your BF. Thanks for reading it.</p>
11 
12 <p>We met on {{met_date}}, and today is {{today}}, so we have been together for {{days}} days.</p>
13 
14 <p>Now you live in {{your_city}}, and I live in {{my_city}}.
15 {% ifequal your_city.lower my_city.lower %}
16 So lucky for living in the same city!
17 {% else %}
18 What a pity for not being together!
19 {% endifequal %}
20 </p>
21 
22 <p>So glad to meet you! You must be the heaven-sent angel for you are
23 <ul>
24 {% for trait in traits %}
25 <li>{{trait}}</li>
26 {% endfor %}
27 </ul>
28 I‘m so fascinated of you!
29 </p>
30 
31 <p>
32 It is now {{weather}} in {{your_city}},
33 {% ifequal weather ‘cold‘%}
34 take care of yourself.
35 {% else %}
36 {% ifequal weather ‘hot‘%}
37 take care of yourself.
38 {% else %}
39 nice weather, isn‘t it?
40 {% endifequal %}
41 {% endifequal %}
42 
43 Hoping for seeing you soon! May you have a pleasent day!
44 </p>
45 
46 <p>Yours Sincerely,<br/>{{ today }}<br/>{{ author }}</p>
47 
48 </body>
49 </html>

  我们有必要对这个模板做解释:

  • {{ name }}表示name变量,所有{{...}}里面包含的是变量。
  • {% ifequal your_city.lower my_city.lower %}为标签(tag),表示if判断语句,判断两个变量是否相等,需要用{% endifequal %}来结束。
  • {% for trait in traits %}为标签,表示for循环语句,traits可以为list或者set序列,需要用{% endfor %}。
  • 其余代码同HTML相同。

  定义好模板之后,我们应该告诉Django怎样使用这个模板,因此,需要在settings.py中的TEMPLATES设置DIRS:

技术分享图片


  这样我们就能在视图中使用该模板了。在相同的文件夹下,新建views.py,代码如下:

 1 from django.shortcuts import render_to_response
 2 import datetime
 3 
 4 def output(request):
 5 c = {name:Rose,
 6 met_date: datetime.datetime(2016,5,24,18,0,0),
 7 today: datetime.datetime.now(),
 8 your_city: shanghai,
 9 my_city: Shanghai,
10 traits: [young and pretty, lovely and positive, warm-hearted and careful, independent and clever],
11 weather: cold,
12 author: Jclian
13 }
14 c[days] = (c[today] - c[met_date]).days
15 return render_to_response(letter.html, c)

其中c为字典,keys为模板中定义的变量,values可以自由定义。
  最后配置好urls.py,并开启8000端口,在网页中输入localhost:8000,得到的页面如下:


技术分享图片



参考文献:

  1. Django中文教程.pdf:http://download.csdn.net/download/huangzhichang13/8177581
  2. Django官方文档之Templates:https://docs.djangoproject.com/en/2.0/topics/templates/

Django学习(3)模板定制

标签:dea   文件夹   rom   margin   oge   min   学习   cin   视图   

原文地址:http://www.cnblogs.com/jclian91/p/8021832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!