我可以在包含标签中使用templatetags吗?

时间:2012-02-26 11:26:03

标签: django django-templates

我写了一个包含标记和两个模板标记,用于将投票代码呈现为html,here you can see

如您所见,有一个vote_buttons_for包含标记用于呈现vote_buttons.html。在模板内部,我尝试在同一个文件中使用 is_up_voted_by is_down_voted_by templatetags。它呈现模板但模板标签不起作用而不会出现任何错误。

你知道为什么会这样发生吗?

1 个答案:

答案 0 :(得分:2)

包含的模板 vote_buttons.html需要{{user}}上下文变量。 vote_button_for不会返回带有“用户”项的词典。假设{{user}}应该是请求用户,您可以使用以下内容:

@register.inclusion_tag('vote_buttons.html',takes_context=True)
def vote_buttons_for(context, object, *args, **kwargs):
    return {
        "user": context['user'],
        "object": object,
        "vote_model": "%s.%sVote" % (
            object._meta.app_label, object._meta.object_name)
    }
相关问题