Debugging NoReverseMatch failures in Django

If you get an error like this:

NoReverseMatch at /account/my_webpage

And it points to and error like:

Reverse for 'password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb64': '7ae9a6e950c08946', u'token': u'4g4-2cbcc2bd7311dc9de06520399f4adbe8'}'

And links to some code like:

{% url 'password_reset_confirm' uidb64=uid token=token %}

This message from Django is really not descriptive. What Django is trying to say is:

I cannot find a matching expression in your file. You will need to add a matching expression like the below to your file.

url(r'^account/password_confirm', views.password_confirm, name="password_reset_confirm"),

However there still an error:

NoReverseMatch at /account/password_reset
1 pattern(s) tried: ['account/password_reset_confirm']

So Django is saying that we have tried a pattern but it does not match. It looks like our pattern does not accept keyword arguments.

Per the Django notes:

Do not mix both positional and keyword syntax in a single call. All arguments required by the URLconf should be present.

We should be good, we do not have mixed arguments and we have just a keyword argument. Let’s set it to handle keyword arguments:

url(r'^account/password_confirm/(?P<uidb64>\w+)/(?P<token>\w+)/', views.password_confirm, name="password_reset_confirm"),

However it still throws error:

NoReverseMatch: Reverse for 'password_reset_confirm' with arguments '()' and keyword arguments '{u'uidb64': '7ae9a6e950c08946', u'token': u'4g4-2cbcc2bd7311dc9de06520399f4adbe8'}' not found. 1 pattern(s) tried: ['account/password_confirm/(?P\\w+)/(?P\\w+)/']

That doesn’t work either, I wonder what is wrong?

Look at the token. Does “\w+” match a dash? NOPE! Let’s match the dash.

url(r'^account/password_confirm/(?P<uidb64>\w+)/(?P<token>[^/]+)/', views.password_confirm, name="password_reset_confirm"),

Now everything works!

Whenever you have a NoReverseMatch error check the following:

  1. Have existing matching url
  2. Match the keyword or positional arguments with that url
  3. Make sure that the argument regular expressions match what you are trying to pass

George Lee

A full stack engineer who has coded on front end, back end, and mobile and embedded system. Have worked on large scale systems at eBay and agile environments at startups. Specialties: Python, PHP, Objective C, Java, Perl, SQL, Javascript, IOS, Android, Amazon EC2, Linux and Windows.

Leave a Reply

Your email address will not be published. Required fields are marked *