-1

Framework Django. I am trying to create a link to the profile of each user (doctor) by creating an automatic url by his id. an error appears: Reverse for 'doctor' with no arguments not found. 1 pattern (s) tried: ['doctor / (? P [^ /] +) / $']

I suppose I need to add a second parameter to the template link, but I can't figure out that "{% url 'doctor' ??%}"

html

 ..."{% url 'doctor'  %}"...

view.py

def doctor(request, pk):
    doctors = Doctor.objects.get(pk)
    return render(request, 'main/doctor.html', {'doctors': doctors})

urls

path('doctor/<str:pk>/', views.doctor, name='doctor'),
Mr How
  • 255
  • 2
  • 10
  • Does this answer your question? [How to add url parameters to Django template url tag?](https://stackoverflow.com/questions/25345392/how-to-add-url-parameters-to-django-template-url-tag) – JPG Nov 08 '20 at 02:21
  • https://docs.djangoproject.com/en/3.1/topics/http/urls/#reverse-resolution-of-urls – iklinac Nov 08 '20 at 02:58
  • Pass the parameter like this: {% url 'doctor' pk=doctors.pk %} – vikash kumar Nov 08 '20 at 03:06

3 Answers3

2

Pass the pk parameter like this {% url 'doctor' doctor.pk %}

also primary keys are usually of the integer type so you could make this correction in your urls.py

path('doctor/<int:pk>/', views.doctor, name='doctor'),
stue
  • 81
  • 2
  • doesnt work, error Reverse for 'doctor' with arguments '('',)' not found. 1 pattern(s) tried: ['doctor/(?P[0-9]+)/$'] – Mr How Nov 08 '20 at 09:19
1

the primary key is a type of integer instant of a string so we need to pass the int in the url.py.

so the url.py should look like this,

path('doctor/<int:pk>/', views.doctor, name='doctor'),

and if you want a specific doctor with some primary key you can pass that in the template context in that case you don't need to query the database with the database manager. just simply pass the primary key to the template and django will handle the rest for you.

in doctor.html,

<a href="{% url 'doctor' pk %}">doctor with special pk </a>

for more information, you can refer to the official doc reverse-resolution-of-urls.

Javed Ali
  • 23
  • 4
  • doesnt work, error Reverse for 'doctor' with arguments '('',)' not found. 1 pattern(s) tried: ['doctor/(?P[0-9]+)/$'] – Mr How Nov 08 '20 at 09:18
1

Probably a typo but it's worth the shot In the views.py file you pass the variable doctors so in your template edit

{% url 'doctor' doctor.pk %}

to

{% url 'doctors' doctors.pk %}

stue
  • 81
  • 2