user9585964
user9585964

Reputation:

django.urls.exceptions.NoReverseMatch on deleteview

I am deleting a post using generic DeleteView. On clicking the link for delete it returns

django.urls.exceptions.NoReverseMatch: Reverse for 'postdelete' with no arguments not found. 1 pattern(s) tried: ['posts/(?P<id>[0-9]+)/delete/$']

I tried placing the before and after /delete/

#urls.py
path('posts/<int:id>/delete/',blogpostDelete.as_view(),name='postdelete'),

#DeleteView
class blogpostDelete(DeleteView):
    success_url='/posts/'   
    template_name="blog/delete.html"
    def get(self,request,id) :
        Blogpost = get_object_or_404(blogpost,id=id)
        return self.render_to_response({'id':id})

#link in template
<a href={% url "postdelete" id=id %}>Delete</a>

#delete.html
{% block content %}

<form action={% url "postdelete" %} method="post">
        {% csrf_token %}
        <p>Are you sure you want to delete "{{ id }}"?</p>
    <input type="submit" value="Confirm">
</form>

{% endblock %}

Upvotes: 1

Views: 264

Answers (2)

Gurmukh Singh
Gurmukh Singh

Reputation: 13

In Delete View No need to pass id in html form action Doing Like this:

 <form method="post">
            {% csrf_token %}
            <p>Are you sure you want to delete "{{ id }}"?</p>
        <input type="submit" value="Confirm">
    </form>

Upvotes: 0

Umair Mohammad
Umair Mohammad

Reputation: 4635

You're not passing id to the url.

Try something like this

<form action={% url "postdelete" id="{{id}}" %} method="post">

Upvotes: 1

Related Questions