【发布时间】:2021-12-05 19:40:57
【问题描述】:
我有这个简单的标签:
myapp/templatetags/my_filters.py
@register.simple_tag
def get_bookmark_object(content_type, object_id):
return Bookmark.objects.get(content_type=content_type, object_id=object_id)
在我的模板中,我希望能够做到这一点:
{% load my_filters %}
{% with object as bookmark %}
{% with bookmark_object=get_bookmark_object bookmark.content_type bookmark.object_id %}
{% if bookmark.content_type.model == 'post' %}
{% include 'content/post/object.html' with object=bookmark_object user_bookmark=bookmark %}
{% elif bookmark.content_type.model == 'note' %}
{% include 'content/note/object.html' with object=bookmark_object user_bookmark=bookmark %}
{% endif %}
{% endwith %}
{% endwith %}
我得到错误:
TemplateSyntaxError at /my-page/
'with' received an invalid token: 'bookmark.content_type'
我的问题是:
如何在with 语句中使用我的自定义get_bookmark_object 模板标签?一个带有代码的示例将帮助我澄清很多。
【问题讨论】:
标签: django django-templates django-template-filters