1

我正在尝试在我的项目中使用这个应用程序。 https://github.com/streema/django-favit 我已经可以使用这个应用程序的 fav-unfav 部分了。我还想为每个用户列出用户的收藏夹。在读我的部分它说使用这个,它会被列出,但我有一个错误

{% with user_favorites <user> "baslik.Entry" as favorite_list %}
    {% for fav_obj in favorite_list %}
        {{ fav_obj }}
    {% endfor %}
{% endwith %}

错误:

TemplateSyntaxError at /
u'with' expected at least one variable assignment

这是 user_favorites 的模板标签部分:

@register.assignment_tag
def user_favorites(user, app_model=None):
    """
    Usage:

    Get all user favorited objects:

        {% with user_favorites <user> as favorite_list %}
            {% for fav_obj in favorite_list %}
                {# do something with fav_obj #}
            {% endfor %}
        {% endwith %}

    or, just favorites from one model:

        {% with user_favorites <user> "app_label.model" as favorite_list %}
            {% for fav_obj in favorite_list %}
                {# do something with fav_obj #}
            {%
        {% endwith %}
    """

    return Favorite.objects.for_user(user, app_model)

我怎样才能摆脱这个错误?谢谢。

4

2 回答 2

1

It's a reasonably common convention in documentation that anything in angle brackets is a placeholder to be replaced by the actual value. In this case, <user> is supposed to be replaced by the object containing the actual user.

{% with user_favorites request.user ... 

I must say, though, that the documentation still doesn't make any sense. You can't use an assignment tag in a with statement like that - even after correcting the user issue, this still won't work. The confusing thing is that the same syntax is repeated throughout the documentation, but it simply doesn't work.

I think this is simply a bug with the documentation, and suspect that if you simply remove the word "with" this will work.

于 2014-10-05T12:38:40.027 回答
0

要在 django 中使用自定义模板标签,需要load在模板中明确显示。在模板的开头添加这一行(但在 之后{% extends ... %},如果有的话):

{% load favit_tags %}

django-favit README 中似乎遗漏了这一步。

于 2014-10-05T11:01:31.640 回答