TransWikia.com

Pass primary key in success_url in Delete Class view in django

Stack Overflow Asked by Ajay kumar on February 14, 2021

I have a Post model in my models.py

In templates, postdescripition.html, I have below code for delete button, In postdescription.html, I referred post object

<a class=type="submit" href="{% url 'delete' pk=post.id %}" >Delete </a> 

In urls.py

path('posts/<int:pk>/delete',views.Delete_Post.as_view(), name = 'delete') ,

In views.py

class Delete_Post(DeleteView):
     model = Post
     template_name = 'dlt.html'
     # dlt.html have an confirmation button
     # success_url= '/profile/1' -> working 
     #i want 1 to should be replaced by user id
     # reverse_lazy('posts') -> working
     #success_url = reverse_lazy('profile/pk=id')-> not working
      success_url= "/posts/"

After deletion I want to redirect to that user profile page, so I need that user.id.., to pass into the view after deletion , I am using "/posts/" in success_url which redirects me to all posts, that’s working fine

urls.py

  path('profile/<pk>/', views.Profile, name='profile'),

But to get id, I tried

  1. request.user.id inside ‘Delete_Post’ class view

request is not defined error

  1. object.author.id

object has no attribute author

Note: I implemented login and authentication. So request.user.id gives current user id

One Answer

The id is not defined here so you cannot use that here, what you should do is handle the user recognition in your Profile view. I am assuming that you would have created a login view etc etc so in your profile view get the user according to the request rather than reversing here with the id

Answered by Amartya Gaur on February 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP