Try this
{% if message.username in message.like_message.all %}
<button> Unlike </button>
{% else %}
<button> Like </button>
{% endif %}
You don’t have to end methods with ‘()’ in templates engine
Try this
{% if message.username in message.like_message.all %}
<button> Unlike </button>
{% else %}
<button> Like </button>
{% endif %}
You don’t have to end methods with ‘()’ in templates engine
try this
def detail_post(request,_detail):
postDetail = UserPosts.objects.get(pk = _detail)
messages = UserMessages.objects.all().filter(post_id=postDetail.id).values()
#This is comment of post
# I tried this but iit is not works
for x in messages:
total = x.like_message.all()
print(total)
context= {
"detail":postDetail,
"messages":messages,
}
I create comments/messages area for my page. and I create like buttons for them too. When user enter the page I want show he default like button(Like or Unlike). If user is in the like list I want show he Unlike but I want to show the Like button if it is not in the list, that is, if it is not liked yet.
Views:
def detail_post(request,_detail):
postDetail = UserPosts.objects.get(pk = _detail) #This is post
messages = UserMessages.objects.all().filter(post_id =postDetail.id) #This is comment of post
# I tried this but iit is not works
for x in messages:
total = x.like_message.all()
print(total)
context= {
"detail":postDetail,
"messages":messages,
}
Template:
{% for message in messages %}
{% if message.username_id == request.user.id %}
<button class="btn btn-primary btn-sm" title="You can't like your message" disabled >Like</button>
{% else %}
<button class="btn btn-primary btn-sm text-white btn-like {{ message.id }} " type="submit" name="usermessages_id"
value="{{ message.id }}" id="{{ message.id }}"> Like </button>
{% endif %}
{% endfor %}
Here is the output of the for loop I wrote in the Views file:
<QuerySet [<User: vahandag>]>
<QuerySet [<User: vahandag>, <User: GladyaTH0R>, <User: user2>, <User: user3>, <User: vahandag1905>]>
<QuerySet []>
<QuerySet []>
<QuerySet [<User: vahandag1905>]>
<QuerySet [<User: vahandag1905>]>
<QuerySet [<User: vahandag>]>
And I want this:
{% if message.username in message.like_message.all() %}
<button> Unlike </button>
{% else %}
<button> Like </button>
{% endif %}
Does anyone have an idea?
thank you sir for help me. the above answer worked for me
filter Returns a new QuerySet containing objects that match the given lookup parameters. values Returns a QuerySet that returns dictionaries, rather than model instances.