Solving Django error ‘NoReverseMatch at’ URL with arguments ‘()’ and keyword arguments ‘{}’ not found.

Django error noreversematch at

Every Django developer encounters below error at least once in their life for sure.

Beginners spend many hours debugging the issue, jumping from question to question on Stackoverflow and posting in multiple groups on Facebook.

In this article we have tried to list all the common mistakes developer makes which leads them to above error.

What is this error:

First lets see what this error is trying to tell developer.

Django is trying to find a URL with name ‘url_name’ and it is not able to find any pattern match.

Application traverses the urls.py  file from top to bottom and match the pattern. It stops at first successful match.

Arguments ‘()’ and keyword arguments ‘{}’ means that the URL application is searching for have no arguments or keyword arguments in it.

Application tried to match against ‘n’ patterns.


Why this error:

Lets say we have below urls.py file.

In above configuration:

  • URL path is articles/<int:year>/ , like /articles/2018/
  • Function name in views.py  file is year_archive
  • URL name is news-year-archive

 

Main reasons behind this error are:

1) Using wrong URL name in template html file.

For example in below template code snippet,

instead of using news-year-archive  are you using news-year-archives

 

2) Commented HTML code.

Django tries to parse the template tags in commented HTML code as well while trying to render the template.

If all the url template tags in your template are correct, check the commented part as well. If there is any error in commented part, remove it.

In the snippet above, application will throw the error for second line as there is no URL with name ‘ news-year-archives ‘.

 

3) Arguments or Keyword arguments

Make sure you are passing the correct number of Arguments or Keyword arguments in URL template tag.

As you can see in urls.py  file, URL with name ‘ news-year-archive ‘ expects an argument ‘year’ in it. If you are not passing the year in url template tag, application will throw the error. So

should be

 

4) Including app URL conf in project urls.py

Make sure you have included the app urls in project urls.py file.

 

5) Namespace

URL namespaces allow you to uniquely reverse named URL patterns even if different applications use the same URL names.

For example app1 have URL name ‘contacts’, now app2 can also have URL with same name provided we are using namespace while including URLs in project’s urls.py  file.

In the template snippet inside url  template tag, we are using namespace:url_name  syntax.

 

6) Incorrect RegEx

If you are using any regular pattern in your URLs, make sure it is correct.

In below URL page_number  should consist only digits (\d+).

 

7) Django Version

In Django 1.10, the ability to reverse a URL by its python path was removed. The named path should be used instead.

 

If your application is still throwing this error, comment here and we will help you sail through this.

 

Host your Django app for free.

 

References:
[1] https://stackoverflow.com/
[2] https://docs.djangoproject.com/en/2.0/topics/http/urls/

(Visited 816 times, 1 visits today)

You must read this :

Leave a Reply

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