本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7575230
建立时间:2006年5月29日




[Django]Django 中自定义 tag 如何获取变量参数的值
软件技术

lhwork 发表于 2007/3/14 9:32:11

这两天学着写了个 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 templatefrom django.template import Context, Template, loader, resolve_variableregister = 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)


阅读全文(7660) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.328 second(s), page refreshed 144766463 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号