I'm running into the issue of displaying the button within for-loop tag. When I remove tags {% for user in users %}
from send_friend.html
, the button is displayed, when I add it again, it's gone.
models.py
class Friend_Request(models.Model):
from_user = models.ForeignKey(
User, related_name='from_user', on_delete=models.CASCADE)
to_user = models.ForeignKey(
User, related_name='to_user', on_delete=models.CASCADE)
views.py
def send_friend_request(request, pk):
from_user = request.user
to_user = User.objects.get(id=pk)
friend_request, created = Friend_Request.objects.get_or_create(
from_user=from_user, to_user=to_user)
if created:
return HttpResponse('friend request sent')
else:
return HttpResponse('friend request already sent')
send_friend.html
<ul>
{% for user in allusers %}
<h1>
{% if not user.is_superuser %}
<p></p>
{% if user not in request.user.friends.all and user != request.user %}
<a href="/send-friend-request/{{user.id}}" class="btn btn--main btn--pill"
>Send Friend Requst</a>
{% elif user in request.user.friends.all %}
<p>You are friends!</p>
{% endif %}
<p></p>
{% endif %}
</h1>
{% endfor %}
</ul>
You need to correctly pass a context to the view. In example:
def your_view(request):
...
context = {"allusers": User.objects.all()}
return render(..., context=context)