How to prevent user to add in palaveri more than once?

wrote "following systems" for the site and there is a problem I can not understand how to allow the user to add in palaveri more than once?
code:
class Followship(models.Model):
 the following = models.ForeignKey(User, related_name="who_follows")
 follower = models.ForeignKey(User, related_name="who_is_followed")


@login_required
def follow(request,user_id=None):
 context = {}
 context['current_user'] = request.user
 selected_user = User.objects.get(id=user_id)
 new_followship_instance = Followship(following=request.user
follower=selected_user)
new_followship_instance.save()
 print("Already save new_followship_instance.")
 return HttpResponse('Complete')

Wanted to do something to solve it via a template but do not know what condition to add if else
July 8th 19 at 12:01
1 answer
July 8th 19 at 12:03
Solution
templatetags/follow_tags.py
@register.simple_tag
def in_follow(user, follower):
 return user.who_follows.filter(follower=follower).exists()

urls.py
url(r'^to_follow/(?P<user_id>\d+)/$', follow name='add_to_follow'),</user_id>

template/user_page_blablabla.html
{% load follow_tags %}
...
{% if messages %}
 <ul class="messages">
 {% for message in messages %}
 <li{% if message.tags %} class="{{ message.tags }}" {% endif>{{ message }}
 {% endfor %}
</li{%></ul>
{% endif %}
...
{% in_follow user current_user as user_in_follow %}
{% if user_in_follow %}
 <span>{% trans "In follow" %}</span>
{% else %}
 <a href="{% url "add_to_follow" user_id="current_user.id" %}">Add to follow</a>
{% endif %}

views.py
@login_required
def follow(request,user_id):
 selected_user = User.objects.get(id=user_id)
 _, created = Followship.objects.get_or_create(the following=request.user
follower=selected_user)
 if created:
 messages.success(request, 'ok')
else:
 messages.error(request, 'User already following')
 return reverse('user_page_blablabla')

models.py
class Followship(models.Model):
 the following = models.ForeignKey(User, related_name="who_follows")
 follower = models.ForeignKey(User, related_name="who_is_followed")
 class Meta:
 unique_together = ('following', 'follower')
in your example, the error 'in_follow' received too many positional arguments - graham commented on July 8th 19 at 12:06
Oh Yes, as I wrote with 1.9 works for django 1.8 - it is necessary instead of @register.to use simple_tag @register.assignment_tag - Emerald_Eichmann commented on July 8th 19 at 12:09

Find more questions by tags DjangoPython