Sunday, December 17, 2017

Re: No Reverse Match Exception



On Dec 17, 2017 12:30 AM, "yingi keme" <yingikeme@gmail.com> wrote:
The problem is that, it works perfectly when i put the number on the template like this

<a href="{% url 'Update' 34 %}">Update</a>

But when i do 

<a href="{% url 'Update' pk=User.id %}">Update</a>

It gives the No Reverse Error. I dont seem to understand why

Instead of User.id, try user.id, capitalization matters.

{% url 'Update' pk=user.id %}

The default context variable provided by django.contrib.auth is 'user' in the template context, not 'User', which I'm assuming you are using.

Since the 'User' variable does not exist in your template context, it resolves to '' (empty string), meaning that you are passing a PK of '' to your view, which it is unhappy with. This is evidenced in the error message you originally posted with an empty string showing as the argument to the URL resolver:

Reverse for 'Update' with arguments '('',)' not found. 1 pattern(s) tried: ['Core/UpdateMe/(?P<pk>\\d+)/$']

HTH,

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWBSsU2uMTSYUFJMy%2BRd_vfqkh_Buq0WNUR_UeiR1h0hw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment