'Dynamic URL's with multible layers
Im making a website using Django, where im making dynamic URLs. However i'm fairly new to programming and haven't been able to find out how to make dynamic URLs with more than 1 parametre.
I'm making a site called "kartotek", which are user specific defined by the varible "kl_id", and therefore has a dynamic URL with following path:
path('kartotek/<str:kl_id>/', views.kartotek, name="kartotek"),
On this page, is there a button with which you choose a patient and come to another URL called "reg" with the following path:
path('reg/<str:kl_id>/<str:pt_id>/', views.reg, name="reg"),
I have the following code in views.py:
def kartotek(request, kl_id):
kliniknavn = Klinik.objects.get(navn=kl_id)
E_patient = kliniknavn.patient_set.all()
context = { 'kliniknavn':kliniknavn, 'E_patient':E_patient}
return render(request,'DentHelp/kartotek.html', context )
def reg(request ,kl_id , pt_id):
kliniknavn = Klinik.objects.get(navn=kl_id)
ptid = Patient.objects.get(id_nr=pt_id)
context = {'ptid':ptid, 'kliniknavn':kliniknavn}
return render(request,'DentHelp/reg.html', context)
I think the problem is the template code, which I'm kinda confused how to make, I was trying this:
{% for patient in E_patient %}
<a href="{% url 'reg' klinik.navn, patient.id_nr %}" class="btn btn-primary">Vælg patient</a>
{% endfor %}
Any have an advise?
Solution 1:[1]
Did you try assigning values explicitly to the appropriate attribute names, like so:
href="{% url 'reg' kl_id=klinik.navn, pt_id=patient.id_nr %}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | maciek.glowka |
