Search code examples
pythonhtmldjangodjango-templatesdjango-urls

What steps to take when debugging 'No reverse match' in Django?


I am getting a 'No reverse match' error in my Django DeleteView template. I have done some research, and this is supposedly caused when an attribute for a url redirect is blank.

The error looks like the following, however the success button redirect works as expected, it is only erroring on my 'cancel' option.

enter image description here

In this case, the url thats generating this error looks like the following:

 <a class="mt-3 mb-3 btn btn-secondary" id="cancel-btn" href="{% url 'post-detail' object.id %}">Cancel</a>

This would mean that, 'object.id' is blank/null...

However, I have added this attribute into a paragraph tag within the same template, and am seeing the correct value print (in this case, 3).

{% block content %}
<div>
    <form method="POST">
        {% csrf_token %} <!--required security token-->
        <fieldset class="form-group">
            <legend class="border-bottom mb-4">Delete Event</legend>
            <p>Are you sure you want to delete the post "{{object.id}}"?</p>
        </fieldset>
        <div class="form-group text-center">
            <button class="btn btn-danger m-3" type="submit">Yes, Delete</button>
            <a class="mt-3 mb-3 btn btn-secondary" id="cancel-btn" href="{% url 'post-detail' object.id %}">Cancel</a>
        </div>
    </form>
</div>
{% endblock content %}

enter image description here

Perhaps I have incorrectly typed my code, but I can't seem to figure out how the value could possibly get erased and is unable to be referenced for the cancel button href?

My urls.py for reference:

urlpatterns = [
path("", EventPostListView.as_view(), name="event-blog-home"),
path("event/<int:pk>/", EventPostDetailView.as_view(), name="post-detail"),
path("event/new/", EventPostCreateView.as_view(), name="post-create"),
path("event/<int:pk>/update/", EventPostUpdateView.as_view(), name="post-update"),
path("event/<int:pk>/delete/", EventPostDeleteView.as_view(), name="post-delete"),
]

All the create, list(post-detail) and update views include this and work as expected.


Solution

  • Thanks to @willeM in the comments, I have solved this issue. Within my template file for DeleteView I was using 'post.pk' as opposed to 'object.pk' as a reference to the model instance.