django no reversematch error

What is NoReverseMatch Error and how to fix it

Django is a powerful programming language that allows you to quickly build websites and web applications in python. Sometimes while running a Django-based website or application, you or your website visitors may get an error saying ‘NoReverseMatch’. In this article, we will learn what is NoReverseMatch error and how to fix it.


What is NoReverseMatch Error and how to fix it

Generally, when Django throws NoReverseMatch Error, you get the following message

The NoReverseMatch exception is raised by django.core.urlresolvers when a matching URL in your URLconf cannot be identified based on the parameters supplied.

It means that Django has been unable to find a URL pattern to match the URL you’ve requested on your page. This happens when you have used url template tag on your Django template and there is no URL in any of your Django’s urls.py files that matches the URL format (name & arguments) used in your URL tag.

To debug this issue, you need to analyze the error message as follows. Look at,

  • NoReverseMatch at /your-url/
    This is the URL that Django is trying to render but it contains a URL that Django is unable to match with any of the URL patterns present in the website/application. So it is unable to render the page.
  • Reverse for ‘url_name’
    This is the URL name that Django is unable to match
  • with arguments ‘()’ and
    These are the non-keyword arguments provided to the URL
  • keyword arguments ‘{}’ not found.
    These are the keyword arguments provided to the URL
  • n pattern(s) tried: []
    These are the patterns Django tried to match but failed to find any matching pattern.

First of all, look at the Django template to find the location where you have mentioned the erroneous URL name. It will be a URL template tag as shown

{% url url_name %}

Once you have found the troublesome url template tag, you can dissect it to find out the actual cause of the problem.

Django can give NoReverseMatch error for the following reasons.

URL names

  • Typo in URL name. Check if the url_name you have specified is actually correct or not.
  • Next, check if there is a URL pattern in any of the urls.py files in your application, with the specified url_name.
  • If you have set app_name in the app’s urls.py (e.g. app_name = ‘sample_app’) or if you included the app with a namespace (e.g. include(‘sample_app.urls’, namespace=’sample_app’), then you need to include the namespace when reversing URL in template, e.g. {% url ‘sample_app:my_url_name’ %} or reverse(‘sample_app:my_url_name’).


Arguments

Even if you have used the correct URL name, you may still get this error, if the number and type of arguments provided in Django template does not match the format required by the URL definition.

The arguments that you pass in your Django template, in your url template tag, should match the capture groups of the URL definition urls.py, where they are present in () brackets.

Take a look at the error message and check if the argument values provided in the Django Template are are correct.

Check if the keyword argument has a typo. If so, then change your URL pattern, or make changes in your Django template. Also check if the value supplied in Django template matches the value expected in URL pattern. For example, if you have used a regex in URL pattern to capture integer arguments, then you will get an error, if you pass a string argument value in Django template.

Also test regex used in URL patterns against actual values passed in Django template. You can use an online regex checker for this purpose. This will help you find out if your values are actually captured correctly by URL pattern.


Django Version

You may also get this error, when you migrate your website/application to a later version of Django. This is because, starting Django 1.10 the ability to reverse match a URL using its python path was removed. You need to use its namepath instead. So if you are using python path, and upgraded your Django, then you will get this error.

In this article, we have learnt what is NoReverseMatch error and how to fix it.

Also read:

How to Create RPM from Script
How to Enable Email Alerts in KeepAlived
How to Use NMAP in Kali Linux
How to Install VirtualBox in Ubuntu
How to Pass Parameter in MySQL Query

Leave a Reply

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