How to pass an ID into a Django template and retrieve data

2.4k Views Asked by At

I am getting stuck passing an id into the template. Here is my views.py file

def add_academy(request,pk):
    child=get_object_or_404(Child_detail,pk=pk)
    academy=Academic.objects.filter(Student_name=child)
    context={
        'academy':academy,
    }

    return render(request,'functionality/more/academy/add.html',context)

Also this my urls.py file

from . import views
from django.urls import path

urlpatterns=[
    path('add_academy/<int:pk>/',views.add_academy, name='add_academy')
]

And this is my template

<div class="container">
    <td><a href="{% url 'add_academy' child.id  %}">
        <button type="button" class="btn btn-primary">Add Academic details of a child</button>
        </a>
    </td>

It shows me an error that states that

NoReverseMatch at /academy/add_academy/3/
Reverse for 'academy' not found. 'academy' is not a valid view function or pattern name.
Request Method: GET
Request URL:    http://127.0.0.1:8000/academy/add_academy/3/
1

There are 1 best solutions below

0
On

maybe you need to set app_name in your urls

from . import views
from django.urls import path

app_name = 'academy'

urlpatterns=[
    path('add_academy/<int:pk>/',views.add_academy, name='add_academy')
]