How to handle two IDs for one endpoint in Django URL patterns and templates?

How to manage two primary keys when constructing urlpatterns in Django for one endpoint(i.e involve two primary keys)

def records(request, patient_id, record_id):
    patient = get_object_or_404(PatientProfile, pk=patient_id)
    medical_record = get_object_or_404(MedicalRecord, pk=record_id)

    
    record_url = reverse('doctor_app:record', kwargs={'patient_id': patient.id, 'record_id': medical_record.id})
    
    print(f"Generated URL: {record_url}") 

  
    return render(request, 'doctor_app/medical_record.html', {
        'patient': patient,
        'medical_record': medical_record,
        'record_url': record_url
    })

Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'record' with arguments '('', '')' not found. 1 pattern(s) tried: ['medical_record/(?P<patient_id>[0-9]+)/(?P<record_id>[0-9]+)/\\Z']
path('medical_record/<int:patient_id>/<int:record_id>/', views.records, name='record'),
"{% url 'doctor_app:record' patient.id medical_record.id %}"

urls.py

urlpatterns = [
path('medical_record/<int:patient_id>/<int:record_id>/', views.records, name='record')
]

template

{% url 'doctor_app:record' patient.id medical_record.id %}
Back to Top