(9)Django框架学习-Templates进阶用法--上

也许,你想要自定义和扩展模板引擎,下面会介绍一些关于如何去扩展模板系统的方法,
了解一下模板系统的工作原理,同时也会介绍Django模板系统中的auto-escapint功能,
这是一种安全机制。

复习一下模板语言的用法

{# 模板tag的用法 #}
{% if done %}
<strong>Over</strong>
{% else %}
<strong>wait</strong>
{% endif %}
{# 模板变量的用法 #}
Now is {{ nowtime }}
在views.py中使用模板的时候:
1. 通过模板名,获得模板对象
2. 创建context对象,类似字典,用于像模板提供变量实际的值
3. 使用context对象进行模板的渲染,返回的是html网页的内容

使用RequestContext对上下文内容进行重用

当渲染一个模板的时候,我们通常使用的是django.template.Context的对象,
这里要介绍另外一个它的子类,django.template.RequestContext,
RequestContext提供了一种把不同的context内容中公共的部分提取出来的方法,
让context的内容重用。
下面来看例子:
1. Context版
from django.template import loader, Context
from django.http import HttpResponse

def view_1(request):
    # ...
    t = loader.get_template('template1.html')
    c = Context({
        'app': 'My app',
        'user': request.user,
        'ip_address': request.META['REMOTE_ADDR'],
        'message': 'I am view 1.'
    })
    return HttpResponse(t.render(c))

def view_2(request):
    # ...
    t = loader.get_template('template2.html')
    c = Context({
        'app': 'My app',
        'user': request.user,
        'ip_address': request.META['REMOTE_ADDR'],
        'message': 'I am the second view.'
    })
    return HttpResponse(t.render(c))
可以看到两个context的内容有些是重复的。比如app, user, ip_address
2. 下面改写成RequestContext版
from django.template import loader, RequestContext
from django.http import HttpResponse

# 使用context processro去提供一些context内容中公共的部分,也就是返回一个字典而已。
def custom_proc(request):
    "A context processor that provides 'app', 'user' and 'ip_address'."
    return {
        'app': 'My app',
        'user': request.user,
        'ip_address': request.META['REMOTE_ADDR']
    }

def view_1(request):
    # ...
    t = loader.get_template('template1.html')
    # 创建方式不同,需要提供的参数有三个,request对象,字典类型,processors可选 
    c = RequestContext(request, {'message': 'I am view 1.'},
            processors=[custom_proc])
    return HttpResponse(t.render(c))

def view_2(request):
    # ...
    t = loader.get_template('template2.html')
    c = RequestContext(request, {'message': 'I am the second view.'},
            processors=[custom_proc])
    return HttpResponse(t.render(c))
可以看到所谓的context processors其实就是一个函数,参数为request,
返回一个字典类型。这就是它所做的所有的事。在这里custom_proc返回的
是包含共同的那三个参数的字典
 
RequestContext构造函数中的第三个参数processors是可选的,可以是
多个custom_proc函数的列表或是元组,在这里我们只传递了一个,可以为多个。
 
结合RequestContext使用render_to_response函数直接返回HttpResponse对象
return render_to_response('template2.html',
        {'message': 'I am the second view.'},
         context_instance=RequestContext(request, processors=[custom_proc]))
以上代码就可以一步到位。
 
但是又引入了另一个问题,在每次使用render_to_response函数时,都要向
RequestContext指定所需要的context processors,因为这个原因,Django又给
我们提供了全局的processors,它默认是被传递给RequestContext对象的,这个设置
对应的是D:\Python27\Lib\site-packages\django\conf\global_setings.py文件
中TEMPLATE_CONTEXT_PROCESSORS属性,这样使用RequestContext的时候,就
不需要每次指定processors了。
我现在使用的是Django-1.4,所以路径是在这里,貌似以前的版本是直接在工程
文件夹下的settings.py文件, 上面都是一些processors函数的字符串表示,下面
介绍一些常用的默认被打开的processors提供的参数:
  • django.contrib.auth.context_processors.auth
user : 当前登入的用户名
perms :用户所有的权限
  • django.core.context_processors.debug
debug:是否打开DEBUG模式
sql_queries:{'sql': ..., 'time': ...}形式的字典信息,显示执行的每一个SQL查询和
它执行所需的时间。从Django的源码可以看出要使用这个processors,需要首先打开调
试模式,其次需要是你的IP在INITERNAL_IPS元组中。
 
INITERNAL_IPS是什么东西?
它在之前的global_settings.py文件中,目前是一个空的元组,应该是用来安全认证的设置(猜测)。
  • django.core.context_processors.request
这就是request对象的processors,估计是直接返回request对象。
源代码和想像的一样。
 
一些关于自定义全局processors的建议:
1. 确保你的每个processor只对应一小部分的功能需要的数据,这样才能保证重用性更强,
组合方式更多,不会太重复。
 
2. 一旦定义了全局processor,那么它将对使用RequestContext的所有模板文件可见,
所以需要注意模板变量名字的选择,一种比较好的作法是全局processor中的变量全部使用大写。
 
3. 习惯把processors函数放在名为context_processors.py文件中,命名习惯而已。只要你在
TEMPLATE_CONTEXT_PROCESSORS元组中写对你的路径就可以。





posted @ 2012-09-01 16:06  btchenguang  阅读(9920)  评论(0编辑  收藏  举报