Django NoReverseMatch Error

Django NoReverseMatch Error

Django’s reverse method can be found under django.core.urlresolvers (new versions: django.urls). To be able to use it you should import it like so:

reverse method is used to generate urls so that you can use them in your template files, model methods, view methods etc.

Why do we use them? We use them not to hard code our urls to adhere to DRY (Don’t Repeat Yourself) principle.

Why am I talking about reverse? Because reverse method is where I encountered this NoReverseMatch error. Error message was something like this:

Reverse for ‘xx’ with arguments ‘()’ and keyword arguments ‘{‘xx’: ‘xx’}’ not found. 1 pattern(s) tried: [‘(?P<xx>[-\\w]+)/(?P<xx>[-\\w]+)/$’]

I replaced the variable names with “xx” above.

Let’s see the bits of code that led to this error:

Above is a method that gets the url for a model that is created in the system. To display the model, I browse to this absolute url address. myapp is the namespace as you will see down below.

As you can see, I am using name spaced  urls. Anyways..

Above is the url patter from my application’s

Above is the url from my main (project) urls file.

As you can see, ultimately the url I am trying to reach is formed by joining these 2 url definition. What you must pay attention for is that there are 2 variables in this structure that needs to be passed to reverse method. And as you can see there is only one variable being passed to the reverse method above, and that is where the problem is.

All I had to do was to see this mistake and provide both variables as kwargs to the reverse method and the problem was gone. Like so:


Hope this helps.
Good Luck,