djangodjango-urls

What is a NoReverseMatch error, and how do I fix it?


I have some code and when it executes, it throws a NoReverseMatch, saying:

NoReverseMatch at /my_url/ Reverse for 'my_url_name' with arguments '()' and keyword arguments '{}' not found. n pattern(s) tried: []

What does this mean, and what can I do about it?


Solution

  • The NoReverseMatch error is saying that Django cannot find a matching url pattern for the url you've provided in any of your installed app's urls.

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

    To start debugging it, you need to start by disecting the error message given to you.

    Start by locating the code in your source relevant to the url that is currently being rendered - the url, the view, and any templates involved. In most cases, this will be the part of the code you're currently developing.

    Once you've done this, read through the code in the order that django would be following until you reach the line of code that is trying to construct a url for your my_url_name. Again, this is probably in a place you've recently changed.

    Now that you've discovered where the error is occuring, use the other parts of the error message to work out the issue.

    The url name

    Arguments and Keyword Arguments

    The arguments and keyword arguments are used to match against any capture groups that are present within the given url which can be identified by the surrounding () brackets in the url pattern.

    Assuming the url you're matching requires additional arguments, take a look in the error message and first take a look if the value for the given arguments look to be correct.

    If they aren't correct:

    If they are correct:

    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 you're still unable to track down the problem, then feel free to ask a new question that includes what you've tried, what you've researched (You can link to this question), and then include the relevant code to the issue - the url that you're matching, any relevant url patterns, the part of the error message that shows what django tried to match, and possibly the INSTALLED_APPS setting if applicable.