Django系列教程:五、模板的使用

注明:python版本为3.3.1、Django版本为1.5.1,操作系统为Windows7,其他版本有一些不同的地方读者可以自行探讨。

第四章我们讲到了模板的一些基本概念和几个常用的函数,下面我们就来讲讲如何使用Django的模板机制来处理静态网页模板。

下面我们用前面几章讲过的内容从新新建一个工程,具体操作代码如下,有不明白的请翻看前面的内容。

django-admin.py startproject testtemplate#创建新的站点
python manage.py runserver#运行web服务

进入testtemplate文件夹再创建template文件夹,然后再在template文件夹内部创建一个html文件,其文件夹结构和html代码如下。
文件结构(tree命令):

└─testtemplate
   │  manage.py
   │
   └─testtemplate
       │  settings.py
       │  urls.py
       │  wsgi.py
       │  __init__.py
       │
       ├─template
       │      template.html
       │
       └─__pycache__
               settings.cpython-33.pyc
               wsgi.cpython-33.pyc
               __init__.cpython-33.pyc

HTML代码:

<html>
<head>
<title>测试Django模板</title>
</head>
<body>
It is now {{ nowdate }}.<br />
<p>我的名字叫{{name}}.</p>
<p>我爱{{favourite}}.</p>
</body>
</html>

好了上面的文档创建好以后,下面就是我们配置使用模板了。

第一步:打开你的settings.py配置文件,找到TEMPLATE_DIRS这项设置吧。 它的默认设置是一个空元组(tuple),加上一些自动生成的注释。其原始代码如下:

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

看到上面的内容,聪明的你应该明白该怎么做了吧,对,就是把放html文件的template文件夹的路径放到上面去,注意上面代码中的示例是templates文件夹,而我们这得文件夹名字叫做template。修改好的代码如下:

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    "D:/Djcode/testtemplate/testtemplate/template",#注意斜杠的方向还有最后面的这个逗号是元组的习惯写法
)

第二步:下面开始写处理代码。我们在和settings同一级的目录下创建一个文件比如:handle.py文件,其功就相当于前面讲的Context()函数。其代码如下:

from django.template.loader import get_template
from django.template import Template,Context
from django.http import HttpResponse
import datetime
def timenow(request):
    now=datetime.datetime.now()
    na="Django"
    fa="编程!"
    t=get_template('template.html')
    html=t.render(Context({'nowdate':now,'name':na,'favourite':fa}))
    return HttpResponse(html)

其中的get_template()函数其实就是将html里卖弄的内容复制过来了,和Template()函数效果差不多。

最后一步:修改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()
#from testtemplate.handle import ttemplate
urlpatterns = patterns('',
    url(r'^testtemplate/$','testtemplate.handle.timenow',name='testtemplate'),
    # Examples:
    # url(r'^$', 'testtemplate.views.home', name='home'),
    # url(r'^testtemplate/', include('testtemplate.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)),     
)

然后打开浏览器输入:http://127.0.0.1:8000/testtemplate/ 即可看到下面的界面。

———————————————————————————————————————

好了,今天就先到这吧,下一章我们将讲解模块的一些知识。

本人既是python的初学者也是Django的初学者,写这一系列教程的目的就是为了加深自己的理解,希望大家多多包涵和指教,有什么问题请留言,谢谢。

鹜落霜洲,雁横烟渚,分明画出秋色。暮雨乍歇,小楫夜泊,宿苇村山驿。何人月下临风处,起一声羌笛。离愁万绪,闲岸草、切切蛩吟似织。 为忆芳容别后,水遥山远,何计凭鳞翼。想绣阁深沉,争知憔悴损,天涯行客。楚峡云归,高阳人散,寂寞狂踪迹。望京国。空目断、远峰凝碧。
原文地址:https://www.cnblogs.com/thunderest/p/3087410.html