1👍
✅
You can check that a relationship exists between the current user and other user tweets
using a custom template filter.
We will write a custom template filter check_relationship_exists
which will take the current user as the argument. This will check if the current tweet object is related to the user passed by performing a filter on its retweet
attribute using user.id
. If there exists a relationship, then the UN REPOST
link will be displayed otherwise a RETWEET
link will be shown.
from django import template
register = template.Library()
@register.filter(name='check_relationship_exists')
def check_relationship_exists(tweet_object, user):
user_id = int(user.id) # get the user id
return tweet_object.retweet.filter(id=user_id).exists() # check if relationship exists
Then in your template, you can do the following:
{% elif tweets|check_relationship_exists:request.user %}
Source:stackexchange.com