现在的位置: 首页 > 综合 > 正文

django第二个项目–使用模板做一个站点访问计数器

2012年10月09日 ⁄ 综合 ⁄ 共 2807字 ⁄ 字号 评论关闭

上一节讲述了django和第一个项目HelloWorld,这节我们讲述如何使用模板,并做一个简单的站点访问计数器。

1、建立模板

  在myblog模块文件夹(即包含__init__.py的文件夹)下面新建一个文件夹templates,用于存放HTML模板,在templates下建立一个index.html

<html>
<body>

<h1>Welcome</h1>

<p>Learning Python and Django Here!</p>
</body>
</html>

2、制定模板路径

  修改myblog模块文件夹的setting.py,将我们之前新建的模板文件夹路径添加到进去,注意元组只有一个元素时要添加一个逗号","

TEMPLATE_DIRS = (
    'myblog/templates',)

3、导入模块

  修改我们之前HelloWorld项目里面的views.py,将模板导入,并设置一个上下文:

#!-*-encoding:utf-8-*-
from django.http import HttpResponse
from django.template import loader,Context

def myHelloWorld(request):
    t = loader.get_template("index.html")#导入模板
    para ={}#上下文参数
    c = Context(para)#上下文
    return HttpResponse(t.render(c))

4、查看模块是否导入成功

  切换到manage.py所在目录

python manage.py runserver

  在浏览器中打开http://127.0.0.1:8000/Hello/,如果没有出现错误的话,应该看到原来的Hello my django 已经变为我们index.html定义的布局

5、使用模板变量进行交互

  修改我们的模板index.html,设置连个模板变量,title和welcome:

<html>
<body>
<title>{{title}}</title>
<h1>{{wlecome}}</h1>

<p>Learning Python and Django Here!</p>
</body>
</html>

6、在view中使用模板变量

  修改我们的views.py,以字典的格式将上下文参数设置为para = {"title":"MyBlog","welcome":"欢迎"},其中键是模板中的变量名,值是我们希望显示的值:

#!-*-encoding:utf-8-*-
from django.http import HttpResponse
from django.template import loader,Context

def myHelloWorld(request):
    t = loader.get_template("index.html")#导入模板
    para = {"title":"MyBlog","welcome":"欢迎"}#上下文参数
    c = Context(para)#上下文
    return HttpResponse(t.render(c))

  模板变量的值可以是字典,字符串,列表,对象,函数,具体看django的template文档

7、查看模板变量是否起作用

  如果刚刚的runserver没有关闭,那么服务器就一直还开着,如果管了,重新启动一次,在浏览器刷新http://127.0.0.1:8000/Hello/

  我们会看到,设置的title变为了"MyBlog",welcome变成了"欢迎",说明模板变量起作用了

8、设置站点访问计数器

  先修改index.html如下:

<html>
<body>
<title>{{title}}</title>

<h1>{{welcome}}</h1>

<p>Learning Python and Django Here!</p>
<li>{{content}}</li>
<p></p>
<li>您是第{{count}}个访问本站的朋友</li>
<li>访问时间:{{time}}</li>
</body>
</html>

  现在我们的模板中有了5个模板变量。

  我们利用一个文件保存已经访问过本站点的次数,修改views.py如下:

#!-*-encoding:utf-8-*-
from django.http import HttpResponse
from django.template import loader,Context

def getTime():#获取当前时间
    import time
    return time.ctime()

def getCount():#获取访问次数
    countfile  = open('count.dat','a+')#以读写形式打开文件
    counttext = countfile.read()   
    try:
        count = int(counttext)+1
    except:
        count = 1    
    countfile.seek(0)
    countfile.truncate()#清空文件
    countfile.write(str(count))#重新写入新的访问量
    countfile.flush()
    countfile.close()
    return count

def myHelloWorld(request):
    t = loader.get_template("index.html")#导入模板    
    time = getTime()
    count = getCount()
    para = {"title":"MyBlog",'welcome':"欢迎","content":"今天我们学习如何制作一个访问站点计数器","count":count,"time":time}
    c = Context(para)#上下文
    return HttpResponse(t.render(c))

  这里面主要添加了两个方法,一个用于获取当前时间,另一个用于获取访问次数。

9、测试我们的站点计数器

  在测试之前我们对URL路径做一个小的修改,将Hello路径换成index路径,这样看书去跟第一个项目有区别些。

 url(r'^index/$',myHelloWorld)

  在浏览器其中刷新http://127.0.0.1:8000/Hello/将会看到404 not found

  

  换成http://127.0.0.1:8000/index/后,我们的站点访问计数器就工作了:

  

  不断的刷新浏览器,你会看到访问次数在增加...

  如果这里出现错误,看错误提示,很可能是乱码错误:

  

  这是因为我们的py文件设置的utf-8格式,而HTML文件用的GBK格式,你可以用NotePad++等编辑器编辑一下html文件的格式,设置为utf-8格式就好了

10、总结

  这一节我们学会了,使用模板和模板变量,当然这里只是一点皮毛,但对我们理解模板有着启蒙的作用。

抱歉!评论已关闭.