how to pass primary key to url django
In your Django urls.py file, define a path that includes a parameter for the primary key:
path('your-url/<int:pk>/', your_view_name, name='your-url-name')
In your views.py file, fetch the object using the primary key, and pass it to the context:
from django.shortcuts import get_object_or_404
from .models import YourModel
def your_view_name(request, pk):
your_object = get_object_or_404(YourModel, pk=pk)
context = {'your_object': your_object}
return render(request, 'your_template.html', context)
In your template, use the primary key in the link:
<a href="{% url 'your-url-name' your_object.pk %}">Link Text</a>