Search code examples
djangodjango-templatesdjango-urlsdjango-users

Django template url NoReverseMatch for re_path


I am attempting to make a validation link e-mail as part of my user sign-up in Django. I have a {% url %} tag in my template however it is giving me a NoReverseMatch error with regards to the uid & token variables.

Here is the code in the template:

https://{{ domain }}{% url 'users:activate' uidb64=uid token=token%}

Here is my URL pattern:

re_path(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', views.activate, name='activate'),

And last of all here is my error:

django.urls.exceptions.NoReverseMatch: Reverse for 'activate' with keyword arguments '{'uidb64': b'MzY', 'token': '4xq-eb603ee3a9676d7b3edc'}' not found. 1 pattern(s) tried: ['users\\/activate/(?P<uidb64>[0-9A-Za-z_\\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$']

I have a hunch it may be something to do with the regular expression but that is not a strong point I have (yet)!

It appears to find the activation URL pattern but I cant seem to make it recognize the arguments for the url!


Solution

  • Looks like uid type is bytes instead of string. Try to decode it before pass it to template context:

    # you view code
    uid = uid.decode()
    context['uid'] = uid
    return render(requet, 'template.html', context)