cstar

eli's docs

   :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

本文接 django: startproject

方法一:

1 创建模板文件

在 project_name/blog 下创建模板目录 templates:

# cd project_name/blog
# mkdir templates

templates 目录中创建模板文件 index.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Django Template</title>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
        <meta name="keywords" content="Django Template" />
        <meta name="description" content="Django Template" />
    </head>
    <body>
        <center>Django Template Learning.</center>
    </body>
</html>
View Code

2 在视图中发布模板

修改 blog/views.py:

from django.http import HttpResponse
from django.template import loader,Context

def index(req):
        t = loader.get_template('index.html')   # auto load file 'index.html' from dir templates
        c = Context({})
        return HttpResponse(t.render(c))

 

方法二:

1 使用 render_to_response() 函数

加载模板、初使化 Context 容器、返回HttpResponse 重复性大,可以直接使用 render_to_response 函数简化:

blog/views.py:

from django.shortcuts import render_to_response

def index(req):
        return render_to_response('index.html', {})

 

posted on 2013-09-27 22:28  exclm  阅读(394)  评论(0编辑  收藏  举报