malisit
malisit

Reputation: 1258

Django TemplateSyntaxError in with template tag

I'm trying to use this app in my project. https://github.com/streema/django-favit I already can use the fav-unfav part of this app. I also want to list favourites of user for every user. In read me part it says use this and it will be listed but I have an error with

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

Error:

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

This is the template tag part for 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)

How can I get rid of this error? Thanks.

Upvotes: 1

Views: 3518

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 600059

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.

Upvotes: 1

stalk
stalk

Reputation: 12054

To use custom template tag in django, it is needed to explicitly load it in template. Add this line at the beginnig of your template (but after {% extends ... %}, if you have such):

{% load favit_tags %}

Looks like this step is missed from django-favit README.

Upvotes: 0

Related Questions