Home > front end >  Django passing data through the url, not finding page in url patterns
Django passing data through the url, not finding page in url patterns

Time:03-16

I'm trying to pass an object's id through the url, but its not able to find the page even after it tries the path when it goes through its patterns

Error:

Using the URLconf defined in GroomingService.urls, Django tried these URL patterns, in this order:

admin/
[name='Home']
appointment-Maker/
account/
admin-home
view_appointment/<id>/
login/ [name='Login Page']
registration/ [name='Registration Page']
logout [name='Logout Page']
The current path, adminview_appointment/21/, didn’t match any of these.

GroomingService.urls

#urls
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', Home_view, name="Home"),
    path('appointment-Maker/', include('appointmentApp.urls')),
    path('account/', include('accountApp.urls')),
    path('admin-home', include('adminApp.urls')),
    path('view_appointment/<id>/', viewAppointment_view), #this is the page it is not finding


    path('login/', Login_view, name='Login Page'),
    path('registration/', Registration_view, name='Registration Page'),
    path('logout', Logout_view, name='Logout Page')
]

adminApp/views.py viewAppointment_view

def viewAppointment_view(request, id):
    appointments = Appointment.objects.get(id = id)
    context = {
        'appointments' : appointments
    }
    return render(request, "admin_templates/viewappointment.html", context)

templates/admin_templates viewappointment.html

{% extends 'base.html' %}

{% block content %}
<a>appointment view</a>
{% endblock %}

templates/admin_templates adminhome.html (the link is clicked through this page)

{% extends 'base.html' %}

{% block content %}
<a>this is the admin page</a>
<br>

{% for a in appointments %}
<a href="adminview_appointment/{{a.id}}/">Client name:{{a.client_dog_name}}</a><br> {% comment %} this is the link that is clicked {% endcomment %}
{% endfor %}


<br>
<a>find month</a>
<form method="POST">
    {% csrf_token %}
    {{ monthyear }}
    <button  type="submit">click</buttom>
    </form>

{% endblock %}

If I'm missing anything please let me know, I had the path at the adminApp/urls.py earlier

urlpatterns = [
    path('', views.adminhome_view, name="Admin Home"),
]

but moved it to where it was trying to find the urls. I have no idea why this might not be working.

CodePudding user response:

It should be view_appointment/{{a.id}}, not adminview_appointment/{{a.id}}, but it is better to make use of the {% url … %} template tag [Django-doc]. You can give the view a name:

path('view_appointment/<int:id>/', viewAppointment_view, name='view_appointment'),

and then refer to it in the template:

<a href="{% url 'view_appointment' id=a.id %}">Client name:{{a.client_dog_name}}</a><br>
  • Related