Django custom assignment_tag没有被执行

fri*_*ixx 3 django django-templates django-1.5 django-1.6

我尝试在Django 1.6中创建自己的赋值模板标签(编辑:我也尝试了1.5.5 w/o成功)但我无法做任何事情.

templatetags/getattribute.py我有:

from django import template
import logging

register = template.Library()
logger = logging.getLogger('wwwcid.website.debug_console')

#@register.assignment_tag
def mytag(context, context_variable):
    raise template.TemplateSyntaxError("buuuuuuuuuuuuuuus")
    logger.debug("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
    print "teeeeeeeeeeeeeeeeeeeest"
    return 'tpppppppppppppppppppppeee'
register.assignment_tag(mytag, takes_context=True)
Run Code Online (Sandbox Code Playgroud)

在我的模板中,profile.html我有:

{% extends "website/base.html" %}
{% load getattribute %}
{% mytag "user.email" as vari %}
{{ vari }}
{% block content %}
<p> Rest of the page. </p>
{% endblock content %}
Run Code Online (Sandbox Code Playgroud)

但是这个标签不起作用.没有错误(即使我自己提出),没有记录输出,没有打印,没有返回,没有.

即使我注册了带有take_context = True的标签,也就是说@register.assignment_tag它不起作用.我在这做错了什么?谢谢你的帮助!

fri*_*ixx 7

好吧,我应该粘贴代码,因为我真的在我的应用程序中有它.我编辑了上面的profile.html部分以反映实际情况.问题是标签和变量只在{% block foo %}和之间执行{% endblock foo %}.因此,使用以下profile.html模板:

{% extends "website/base.html" %}
{% load getattribute %}
{% block content %}
{% mytag "user.email" as vari %}
{{ vari }}
<p> Rest of the page. </p>
{% endblock content %}
Run Code Online (Sandbox Code Playgroud)