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

这两天学着写了个 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)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值