这一章我们开始介绍模板,在前一章中,你可能已经注意到我们在例子视图中返回文本的方式有点特别。 也就是说,HTML被直接硬编码在 Python 代码之中。这样的确有点BT。
def current_datetime(request): now = datetime.datetime.now() html = "<html><body>It is now %s.</body></html>" % now return HttpResponse(html)
<html> <head><title>订单</title></head> <body> <h1>订单</h1> <p>尊敬的 {{ person_name }},</p> <p>感谢来您订购的来自 {{ company }} 的订单. 订单时间为: {{ ship_date|date:"F j, Y" }}.</p> <p>这里是您的订货列表:</p> <ul> {% for item in item_list %} <li>{{ item }}</li> {% endfor %} </ul> {% if ordered_warranty %} <p>您的保修信息将包含在包裹中。</p> {% else %} <p>你没有定购保修要求, 所以,如果机器停止运行您将自己负责。</p> {% endif %} <p>张三,<br />{{ company }}</p> </body> </html>用两个大括号括起来的文字(例如 {{ person_name }} )称为 变量(variable) 。这意味着在此处插入指定变量的值。
TEMPLATE_DIRS = ("Bidding/templates" )其实这里的目录以及路径可以自己制定,只需要在setting里面设置一下就ok了,这里也可以使用绝对路径如 :
TEMPLATE_DIRS = ( ‘C:/www/django/templates‘, )完成 TEMPLATE_DIRS 设置后,下一步就是修改视图代码,让它使用 Django 模板加载功能而不是对模板路径硬编码。 返回 current_datetime 视图,进行如下修改:
from django.shortcuts import render_to_response import datetime def current_datetime(request): now = datetime.datetime.now() return render_to_response(‘current_datetime.html‘, {‘current_date‘: now})
from django.shortcuts import render_to_response import datetime def current_datetime(request): current_date = datetime.datetime.now() return render_to_response(‘current_datetime.html‘, locals())把所有的模板都存放在一个目录下可能会让事情变得难以掌控。 你可能会考虑把模板存放在你模板目录的子目录中,这非常好。也可以这样:
return render_to_response(‘dateapp/current_datetime.html‘, locals())Windows用户必须使用斜杠而不是反斜杠。说了这么多,我想你一定和我一样想试试这个模板了。上面已经介绍了views.py的修改内容,下面我们来创建一个模板current_datetime.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html lang="en"> <head> <title>显示当前时间</title> </head> <body> <h1>这仅仅是一个简单的例子</h1> <hr> <p>当前时间为:{{ current_date }}</p> </body> </html>
from django.shortcuts import render_to_response import datetime def current_datetime(request): current_date = datetime.datetime.now() return render_to_response(‘current_datetime.html‘, locals())然后上传代码,看效果把。
<div style="background:#eeeeee;width:100%;height:60px;"> 学习python是个快乐的过程----{{ author }} </div>
<div style="background:#eeeeee;width:100%;height:30px;"> 版权所有----{{ author }} </div>然后我们再建立一个新的模板,把他放在template根目录里面,命名为template_include.html
template_include.html:
<html> <body> {% include "base/top.html" %} <h1>再过 {{ offset }} 个小时, 时间将会是 {{ dt }}.</h1> {% include "base/bottom.html" %} </body> </html>然后我们在修改一下之前的 hours_ahead 视图:
def hours_ahead(request, offset): try: offset = int(offset) except ValueError: raise Http404() dt = datetime.datetime.now() + datetime.timedelta(hours=offset) author="hemeng80" return render_to_response(‘template_include.html‘, locals())到目前为止,我们的模板范例都只是些零星的 HTML 片段,但在实际应用中,你将用 Django 模板系统来创建整个 HTML 页面。 这就带来一个常见的 Web 开发问题: 在整个网站中,如何减少共用页面区域(比如站点导航)所引起的重复和冗余代码?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html> <head> <title>{% block title %}{% endblock %}</title> </head> <body> <h1>这里是一个学习用的测试网站</h1> {% block content %}{% endblock %} {% block footer %} <hr> <p>感谢你的访问</p> {% endblock %} </body> </html>现在我们已经有了一个基本模板,我们可以在建立一个 hello_base.html 模板来 使用它:
{% extends "base.html" %} {% block title %}欢迎{% endblock %} {% block content %} <p>这时我的第一个Python程序: {{ MyString }}.</p> {% endblock %}在改造一下views.py:
# -*- coding: utf-8 -*- from django.http import HttpResponse from django.shortcuts import render_to_response import datetime def hello(request): return HttpResponse(‘Hello world‘) def current_datetime(request): current_date = datetime.datetime.now() return render_to_response(‘current_datetime.html‘, locals()) def hours_ahead(request, offset): try: offset = int(offset) except ValueError: raise Http404() dt = datetime.datetime.now() + datetime.timedelta(hours=offset) author="hemeng80" return render_to_response(‘template_include.html‘, locals()) def hello_base(request): MyString = ‘Hello world‘ return render_to_response(‘hello_base.html‘, locals())在配置下urls.py:
from django.conf.urls import patterns, include, url # Uncomment the next two lines to enable the admin: # from django.contrib import admin # admin.autodiscover() urlpatterns = patterns(‘‘, # Examples: # url(r‘^$‘, ‘Bidding.views.home‘, name=‘home‘), # url(r‘^Bidding/‘, include(‘Bidding.foo.urls‘)), # Uncomment the admin/doc line below to enable admin documentation: # url(r‘^admin/doc/‘, include(‘django.contrib.admindocs.urls‘)), # Uncomment the next line to enable the admin: # url(r‘^admin/‘, include(admin.site.urls)), url(r‘^hello/$‘, ‘Bidding.views.hello‘), url(r‘^time/$‘, ‘Bidding.views.current_datetime‘), url(r‘^time/plus/(\d{1,2})/$‘, ‘Bidding.views.hours_ahead‘), url(r‘^hello_base/$‘, ‘Bidding.views.hello_base‘), )在看看程序的运行结果,你一定可以了解模板的包含、继承的意义了!
Python+Django+SAE系列教程10-----Django的模板,布布扣,bubuko.com
Python+Django+SAE系列教程10-----Django的模板
原文:http://blog.csdn.net/hemeng1980/article/details/24265157