Home > Enterprise >  Why am I getting a No Reverse Match error?
Why am I getting a No Reverse Match error?

Time:09-20

I'm trying to add delete functionality to my Django project, and I seem to be stuck in a loop whenever I try to access my health_hub_history page.

I keep getting the below error, which I know is to do with my urls.py setup, but I have commented it out- so I really don't know why I'm still getting the error!

Error message: NoReverseMatch at /MyHealth/history Reverse for 'delete_entry' not found. 'delete_entry' is not a valid view function or pattern name.

Views.py:

from django.shortcuts import render, get_object_or_404, redirect
from django.views import View, generic
from .models import HealthStats
from .forms import StatUpdateForm


def home(request):
    return render(request, 'home.html')


def health_hub(request):
    latest = HealthStats.objects.filter(user=request.user).latest('date')
    context = {
            "user": latest.user,
            "weight": latest.weight,
            "date": latest.date,
            "run_distance": latest.run_distance,
            "run_time": latest.run_time,
        }
    return render(request, 'health_hub.html', context)


def health_history(request):
    serialized_stats = []
    for stats in HealthStats.objects.filter(user=request.user):
        serialized_stats.append({
            "user": stats.user,
            "weight": stats.weight,
            "date": stats.date,
            "run_distance": stats.run_distance,
            "run_time": stats.run_time,
        })
    context = {
        "stats": serialized_stats
        }
    return render(request, 'health_hub_history.html', context)
 

class UpdateHealth(View):
    
    def get(self, request, *args, **kwargs):

        stats = HealthStats
        update_form = StatUpdateForm
             
        context = {
            'stats': stats,
            'update_form': update_form,
            'user': stats.user,
            'weight': stats.weight,
            'date': stats.date,
        }
        return render(request, 'health_hub_update.html', context)
    
    def post(self, request, *args, **kwargs):

        stats = HealthStats
        update_form = StatUpdateForm(data=request.POST)
             
        context = {
            'stats': stats,
            'update_form': update_form,
            'user': stats.user,
            'weight': stats.weight,
            'date': stats.date,
            'run time': stats.run_time,
            'run distance': stats.run_distance
        }

        if update_form.is_valid():
            update_form.save()
            
        return render(request, 'health_hub_update.html', context)


# def delete_entry(request, entry_id):
#     entry = get_object_or_404(HealthStats, id=entry_id)
#     entry.delete()
#     return redirect("health_hub_history")

urls.py:

from django.urls import path
from django.contrib.staticfiles.storage import staticfiles_storage
from django.views.generic.base import RedirectView
from . import views

app_name = 'HealthHub'

urlpatterns = [
    path('', views.home, name='home'),
    path('MyHealth/', views.health_hub, name='health_hub'),
    path('MyHealth/update', views.UpdateHealth.as_view(), name='health_hub_update'),
    path('MyHealth/history', views.health_history, name='health_hub_history'),
    path('favicon.ico', RedirectView.as_view(url=staticfiles_storage.url("favicon.ico"))),
    # path('MyHealth/delete_entry/<id>', views.delete_entry, name='delete_entry'),
]

health_hub.html (button from which the error occurs):

<a href="{% url 'HealthHub:health_hub_history' %}"><button >View my Health
                History</button></a>

health_hub_history.html:

{% extends 'base.html' %}
{% load static %}

{% block content %}
<div >
    <div >
        <div >
            <h1>My Health History</h1>
        </div>
    </div>
</div>
<div >
    <div >
        <div >
            <table >
                <tr>
                    <td>User:</td>
                    <td>Weight (lbs):</td>
                    <td>Date:</td>
                    <td>Run Distance (km):</td>
                    <td>Run Time (HH:MM:SS):</td>
                    <td>Action</td>
                </tr>
                {% for stat in stats %}
                <tr>
                    <td>{{ stat.user }}</td>
                    <td>{{ stat.weight }} </td>
                    <td>{{ stat.date }}</td>
                    <td>{{ stat.run_distance }}</td>
                    <td>{{ stat.run_time }}</td>
                    <!-- Button trigger modal -->
                    <td>
                        <button type="button"  data-bs-toggle="modal"
                            data-bs-target="#staticBackdrop">
                            Delete
                        </button>
                    </td>
                    <!-- Modal -->
                    <div  id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false"
                        tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
                        <div >
                            <div >
                                <div >
                                    <h5  id="staticBackdropLabel">Are you sure you want to delete
                                        this entry?</h5>
                                    <button type="button"  data-bs-dismiss="modal"
                                        aria-label="Close"></button>
                                </div>
                                <div >
                                    If you just need to amend something, try the edit button.
                                </div>
                                <div >
                                    <button type="button" 
                                        data-bs-dismiss="modal">Cancel</button>
                                    <!-- <a href="{% url 'delete_entry' entry_id %}"><button >I'm -->
                                            sure</button></a>
                                </div>
                            </div>
                        </div>
                    </div>
                </tr>
                {% endfor %}
            </table>
        </div>
    </div>
</div>
{% endblock content %}

I don't understand how it can all be commented out, but it's still trying to access the 'delete_entry' view?

CodePudding user response:

Django does not ignore HTML comments, you need to use a special tag :

{% comment "Optional note" %}
    <a href="{% url 'delete_entry' entry_id %}"><button >
{% endcomment %}

https://docs.djangoproject.com/en/4.1/ref/templates/builtins/#comment

  • Related