success url does not work in my class based DeleteView

  django, django-templates, django-views, python

My success_url for my class based delete view does not work for some reason.

in views.py

# allows a user to delete a project
class DeletePost(DeleteView):
    template_name = 'user_posts/post_delete.html'
    model = Post
    # return to the all posts list
    success_url = reverse_lazy('posts_list')

# make sure the user is looking at its own post
def dispatch(self, request, *args, **kwargs):
    obj = self.get_object()
    if not obj.user == self.request.user:
        raise Http404("You are not allowed to Delete this Post")
    return super(DeletePost, self).dispatch(request, *args, **kwargs)

in urls.py:

path('list/', PostsListView.as_view(), name="posts_list"),
path('create-post/', CreatePostView.as_view(), name="post_create"),
path('update-post/<int:pk>', UpdatePost.as_view(), name="post_update" ),
path('delete-post/<int:pk>', DeletePost.as_view(), name="post_delete")

in the HTML file:

{% extends 'base.html' %}
{% block content %}

<form action="." method="POST" style="width:80%;">
     {% csrf_token %}
     <h3>Do You want to delete this post: "{{ object.title }}"</h3>

     <input class="btn btn-primary" type="submit" value="Confirm"/>
     <a href="{% url 'posts_list' %}">Cancel</a>

</form>
{% endblock content %}

whenever I click ok to delete a specific project, it doesn’t return to the list of posts for:

image of the error on the webpage

Source: Python Questions

LEAVE A COMMENT