Django 中自定义 tag 如何获取变量参数的值

来源:互联网 发布:汽车修理软件哪个好 编辑:程序博客网 时间:2024/05/02 02:31
这两天学着写了个 Django 的 tag,这个 tag 叫做 "post_detail_tag", 调用的代码像这样:

{% post_detail_tag post.id current_post_id %}

其中后面两个都是传递的变量。

结果我发现在 tag 的代码中,一开始解析传递过去的 token 时,能获取到的仅仅是 "post.id", "current_post_id" 这种变量名,而无从获取其变量值。

当然了,仔细想想的确也应该这样,因为这个时候还没有 context. 传进 tag 的仅仅是 "post_detail_tag post.id current_post_id" 这个字符串罢了,还在 tokenize 的低级阶段。

因此自然想到,实际的变量值获取必须在 tag 对应的 Node 类的 render 方法中才可以获取到,因为这个时候参数中会传进来 context.

奇怪的是获取变量值这样一个常见的需求却没有在文档中看到说明,相关范例代码里也找不到。也许是我看的不仔细吧。。

翻看 D:/Python24/Lib/site-packages/Django-0.95.1-py2.4.egg/django/template/__init__.py 的源代码才找到 resolve_variable 这个函数。另外对于 FilterExpression 而言也有 resolve 函数,但这个内部还是调用 resolve_variable 的。因此导入这个函数试了一下,发现可以取到变量的值了。

代码:
from django import template
from django.template import Context, Template, loader, resolve_variable

register 
= template.Library()

class PostDetailNode(template.Node):
    
def __init__(self, id, current_post_id):
        self.id 
= id
        self.current_post_id 
= current_post_id        
        
pass
    
    
def render(self, context):       
        current_post_id 
= int(resolve_variable(self.current_post_id, context))        
        context[
'current_post_id'= current_post_id
        t 
= loader.get_template("forum/templatetags/post_detail.html")        
        
return t.render(context)

#@register.tag(name='post_detail_tag')
def do_post_detail_tag(parser, token):
    
try:
        
#tag_name, args = token.contents.split(None, 1)
        #id, current_post_id = args.split(None, 1)
        tag_name, id, current_post_id = token.split_contents()
        
print id, current_post_id
    
except ValueError:        
        
raise template.TemplateSyntaxError, "%s tag requires argument" % tag_name
    
    
return PostDetailNode(id, current_post_id)

register.tag(
'post_detail_tag', do_post_detail_tag)
原创粉丝点击