2

In my html, I want to send this values to my filter but I got this error :

ValueError at / invalid literal for int() with base 10: '{{request.user.id}}'

My HTML:

<span><i{% if post|liked:"{{request.user.id}},{{post.id}}" %} class="fa fa-heart pointer" {% else %} class="fa fa-heart-o pointer" {% endif %} aria-hidden="true"></i> <span class="text-dark">{{ post.likes.count }}</span>

My Filter:

@register.filter()
@stringfilter
def liked(value, args):
    values = args.split(',')
    user_id, post_id = values
    user = User.objects.get(id=user_id)
    post = Post.objects.get(id=post_id)
    is_liked = post.likes.get(id=user_id)
   
    if is_liked != None:
        return True
    else:
        return False

Thanks for any answer.

Biplove Lamichhane
  • 3,995
  • 4
  • 14
  • 30
cksylr
  • 61
  • 5

1 Answers1

1

It's just a user.id not request.user.id in the template: So,

<span><i{% if post|liked:"{{user.id}},{{post.id}}" %} class="fa fa-heart pointer" {% else %} class="fa fa-heart-o pointer" {% endif %} aria-hidden="true"></i> <span class="text-dark">{{ post.likes.count }}</span>

EDIT

As filter is not taking variable as variable. So, this could be the way:

{% with args=user.id|add:", "|add:post.id %}
    <span><i{% if post|liked:args %} class="fa fa-heart pointer" {% else %} class="fa fa-heart-o pointer" {% endif %} aria-hidden="true"></i> <span class="text-dark">{{ post.likes.count }}</span>
{% endwith %}

See with

Biplove Lamichhane
  • 3,995
  • 4
  • 14
  • 30
  • What is the error now? Error must have differed? show complete traceback for the change. – Biplove Lamichhane Oct 10 '20 at 10:14
  • @cksylr Do not forget to upvote or accept answer. If your problem is solved or is helpful. Make use of this feature. It encourages us as well as it helps other person to know the status about the post. Not only on the post but throught the Stack Overflow network. Thanks :) – Biplove Lamichhane Oct 10 '20 at 10:37
  • I tried but my reputation is lower than 15, so i can't upvote yet. – cksylr Oct 10 '20 at 10:41
  • Well, I forgot about that. Lol :):) But, should be able to accept the answer though ?? – Biplove Lamichhane Oct 10 '20 at 10:43