1

I'm making a wishlist-type site where the row with the user's last change is highlighted. However, I'm having trouble doing that, as apparently using '==' on a context variable and the object's ID is not returning true, despite them both looking the same.

For example, this is the render call I'm making:

def wishlist(request):
    ...
    user_change = request.COOKIES.get('voted_or_added', False)
    return render(request, 'xbox_wishlist/wishlist.html', { 'user_change': user_change, })

Then in my template, I have:

...
<tbody>
    {% for game in ranked_wishlist %}
        {% if user_change == game.id %}
    <tr style="background: #80FF80;">
        {% else %}
    <tr>
        {% endif %}
...

I've tried showing both {{ game.id }} and {{ user_change }}, and they both show the same number at the correct place, but that if statement's predicate doesn't appear to be evaluating to True ever. I've tried replacing the == with in, I've tried swapping their positions, I've tried everything but nothing seems to work.

Any ideas?

Staunch
  • 259
  • 3
  • 12

1 Answers1

1

From the Django documentation

HttpRequest.COOKIES

A standard Python dictionary containing all cookies. Keys and values are strings.

So the issue is that user_change is a string, but game.id is an integer. In python an integer, and its string representation are not equal.

Try this instead:

user_change = int(request.COOKIES.get('voted_or_added', -1))
return render(request, 'xbox_wishlist/wishlist.html', { 'user_change': user_change, })
Community
  • 1
  • 1
  • Ugh, that did it. This had occurred to me, but i wasn't sure how to convert to int/string inside the template... didn't think to do it before passing it in—thank you! – Staunch Jul 04 '14 at 03:55
  • [There are some tips to do that in this question](http://stackoverflow.com/questions/4831306/need-to-convert-a-string-to-int-in-a-django-template), but in general, you want to keep that kind of logic in your view, not your template. –  Jul 04 '14 at 03:57