在上一节课,我们看到视图中返回HttpResponse的方式比较操蛋,HTML被直接硬编码在Python代码中。尽管这种技术有利于我们更好的了解视图的工作方式,但是如果实际工作中这样做肯定是不行的。原因:
1:对页面进行修改的时候,必须对Python代码进行修改,因为网站设计修改比底层代码修改频繁地多,因此如果可以在不修改Python代码的时候修改网站设计,这样就会方便的多。
2:Python代码编写和HTML设置是亮相不同的工作,因该由不同的人或者部门来完成。
3:各司其职,效率最高。

<html> <head><title>Ordering notice</title></head> <body> <h1>Ordering notice</h1> <p>Dear {{ person_name }},</p> <p>Thanks for placing an order from {{ company }}. It's scheduled to ship on {{ ship_date|date:"F j, Y" }}.</p> <p>Here are the items you've ordered:</p> <ul> {% for item in item_list %} <li>{{ item }}</li> {% endfor %} </ul> {% if ordered_warranty %} <p>Your warranty information will be included in the packaging.</p> {% else %} <p>You didn't order a warranty, so you're on your own when the products inevitably stop working.</p> {% endif %} <p>Sincerely,<br />{{ company }}</p> </body> </html>
{{}}中的东西是变量,由view视图传过去。
{% %} 是模板标签。标签的定义比较明确,即:通知模板系统完成某些工作的标签。
for 标签类似于Python里面的for语句,可以让我们循环访问序列中的每一个项目。
if 标签,正如你所料,是用来执行逻辑判断的。 在这里,tag标签检查ordered_warranty值是否为True。如果是,模板系统将显示{% if ordered_warranty %}和{% else %}之间的内容;否则将显示{% else %}和{% endif %}之间的内容。{% else %}是可选的。最后,这个模板的第二段中有一个关于filter过滤器的例子,它是一种最便捷的转换变量输出格式的方式。 如这个例子中{{ship_date|date:"F j, Y" }},我们将变量ship_date传递给date过滤器,同时指定参数"F j,Y"。date过滤器根据参数进行格式输出。 过滤器是用管道符(|)来调用的。
def about_me(reques,namet) : return render(request, 'aboutme.html',{‘name’:name,'error':False})
这里出了一点问题,我们先跳过视图,去学校模板,解决之后继续更新视图。