Hello!
I have a problem that I cannot solve.
Background:
I’m developing a website with user authentication and profile pages. I have renderad all posts that a user have created on the users profil page. There is also options to update or delete a certain post. However, the delete option is not working properly. I know the cause of the error but I have not solution to it.
Before I attempt to explain the issue, I’ll share my codes.
View functions:
def profilepage(request, pk):
user = Member.objects.get(id=pk)
profilePageAdds = Room.objects.values("id", "title")
context = {"profilePageAdds": profilePageAdds, "user": user}
return render(request, "profile_app/profilepage.html", context)
def deleteRoom(request, pk):
room = Room.objects.get(id=pk)
room.delete()
return redirect("profilepage_app:profilepage")
def updateRoom(request, pk):
updateRoom = Room.objects.get(id=pk)
if request.method == 'POST':
updateRoom.title = request.POST.get('title')
updateRoom.save()
return redirect('profilepage_app:profilepage')
context = {"updateRoom": updateRoom}
return render(request, "profile_app/profilepage.html", context)
URL patterns:
app_name = "profilepage_app"
urlpatterns = [
path("profil/<str:pk>/", views.profilepage, name="profilepage"),
# Path for CRUD Ops
path("profil/adds/delete/<int:pk>/",
views.deleteRoom, name="deleteRoom"),
path('post/<int:pk>/update/', views.updateRoom, name='updateRoom'),
HTML code:
<div class="m-1 d-flex flex-column">
<table class="table table-striped">
<thead>
<tr>
<th scope ="col" class="fs-3 ms-1 mt-1">
My posts
</th>
</tr>
</thead>
<tbody>
{% for add in profilePageAdds %}
{% if request.user == user %}
<tr>
<td class="px-2 fs-4 d-flex justify-content-between">
<a href="#" class="text-decoration-none">{{add.title}}</a>
</td>
<td>
<a href="{% url 'profilepage_app:updateRoom' add.id %}">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-clockwise mx-2" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M8 3a5 5 0 1 0 4.546 2.914.5.5 0 0 1 .908-.417A6 6 0 1 1 8 2v1z"/>
<path d="M8 4.466V.534a.25.25 0 0 1 .41-.192l2.36 1.966c.12.1.12.284 0 .384L8.41 4.658A.25.25 0 0 1 8 4.466z"/>
</svg>
</a>
<a href="{% url 'profilepage_app:deleteRoom' add.id %}">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash mx-2" viewBox="0 0 16 16">
<path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5Zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5Zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6Z"/>
<path d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1ZM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118ZM2.5 3h11V2h-11v1Z"/>
</svg>
</a>
</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
As you can see, I am quering the Member ID in the profilepage-function. In the profile page, when a certain post must be deleted, the error that I receive states:

- How can a user delete a certain post and return back to users profile page where only the existing post are displayed?
- Can I somehow use the PK from the profilepage-function in the deleteRoom-function?
- Do I use reverse() in my deleteRoom-function or what can I do?