Issue
I am working on a project but after trying multiple ways I still couldn't find the solution. I need to get the current room on the page so that user can leave room, I am aware that it has to do with making queries or using pk,id. (My 1st question ever, sorry if it is not correct way to ask).
This is the code from Views.py:
def leave_room(request, room):
room = request.GET['room.pk']
room_details = Room.objects.get(name=room)
messages = Messages.objects.filter(room=room_details.pk)
membership = RoomMember.objects.filter(user=request.user, room=messages)
membership.delete()
return redirect("/")
urls.py:
path("leave/<int:pk>/join/", views.leave_room, name="leave_room"),
html:
<a class="btn btn-danger" href="{% url 'leave_room' pk=room.pk %}">Leave Room</a>
models.py:
class Room(models.Model):
name = models.CharField(max_length=100)
about = models.TextField(max_length=500, null=True, blank=True)
creator = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name='room_creator')
members = models.ManyToManyField(User, through="RoomMember")
class RoomMember(models.Model):
approved = models.BooleanField(default=False, blank=False)
room = models.ForeignKey(Room, related_name='memberships', on_delete=models.CASCADE)
user = models.ForeignKey(User, related_name='user_groups', on_delete=models.CASCADE)
class Messages(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=False, blank=False)
text = models.CharField(max_length=10000, blank=False, null=False)
date = models.DateTimeField(default=datetime.now)
room = models.ForeignKey(Room, null=True, blank=False, on_delete=models.CASCADE)
Solution
Since you have a parameter <int:pk>
the primary key of the room is passed through the pk
parameter. This thus means that leave_room
should be implemented as:
from django.contrib.auth.decorators import login_required
from django.views.decorators.http import require_POST
@require_POST
@login_required
def leave_room(request, pk):
nitems, __ = RoomMember.objects.filter(
user=request.user, room_id=pk
).delete()
if not nitems:
raise Http404
else:
return redirect('name-of-some-view')
The removal link is thus a mini form to make a POST request to the view:
<form method="post" action="{% url 'leave_room' pk=room.pk %}">
{% csrf_token %}
<button type="submit" class="btn btn-danger">Leave Room</button>
</form>
Note: Section 9 of the HTTP protocol specifies that requests like GET and HEAD should not have side-effects, so you should not change entities with such requests. Normally POST, PUT, PATCH, and DELETE requests are used for this. In that case you make a small
<form>
that will trigger a POST request, or you use some AJAX calls.
Note: You can limit views to a view to authenticated users with the
@login_required
decorator [Django-doc].
Answered By - Willem Van Onsem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.