什么是NoReverseMatch错误,我该如何解决? [英] What is a NoReverseMatch error, and how do I fix it?

查看:229
本文介绍了什么是NoReverseMatch错误,我该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,当它执行时,它会抛出一个NoReverseMatch,说:


NoReverseMatch at / my_url / Reverse for'my_url_name '与参数'()'和关键字参数'{}'找不到。 n模式尝试:[]


这是什么意思,我该怎么办?

解决方案

NoReverseMatch 错误是说,Django找不到您在任何已安装的应用程序的网址中提供的URL的匹配url模式。


当您的URLconf中匹配的URL不能根据提供的参数进行识别时,django.core.urlresolvers会引发NoReverseMatch异常。


要开始调试,您需要首先解释给您的错误信息。




  • / my_url /

    中的NoReverseMatch

    这是目前正在呈现的网址,这是您的应用程序当前正在尝试访问的URL,它包含一个不能匹配的网址


  • 反向my_url_name'



    这是无法找到的url的名称


  • 带参数' )'和



    这些是提供给url的非关键字参数


  • 关键字参数'{}' 未找到。



    这些是提供给url的关键字参数


  • []



    这些模式可以在您尝试与

  • $ b $匹配的urls.py文件中找到b


首先查找与您正在呈现的网址相关的源代码 - url,视图和所涉及的任何模板。在大多数情况下,这将是您正在开发的代码的一部分。



完成此操作后,按照django将遵循的顺序阅读代码,直到找到正在尝试为您的 my_url_name 。再次,这可能是您最近更改的地方。



现在您已经发现错误发生的位置,请使用错误消息的其他部分解决问题。



网址名称




  • 是否有打字错误?您是否提供了您要访问给定名称的URL?

  • 如果您将该应用包含在命名空间中(例如 include('myapp.urls',namespace ='myapp'),那么您需要在反转时包含命名空间,例如 {%url'myapp:my_url_name' %} reverse('myapp:my_url_name')



参数和关键字参数



参数和关键字参数用于与给定网址中存在的任何捕获组进行匹配,可以通过围绕()括号。



假设您匹配的网址需要参考参数,请查看错误消息,并首先查看给定参数的值是否正确。



如果它们不正确:




  • 缺少值或空字符串



    这通常意味着您传递的值不包含您期望的值。看看你为它分配价值,设置断点,你需要弄清楚为什么这个值不能正确传递。


  • 关键字参数有一个打字错误



    在url模式或正在构建的url中更正这一点。




如果它们是正确的:




  • 调试正则表达式



    您可以使用正则表达式等网站快速测试您的模式是否匹配网址您认为您正在创建,将网址格式复制到顶部的正则表达式字段中,然后使用文本区域来包含您认为应该匹配的任何网址。



    常见错误:




    • 匹配通配符或任何其他正则表达式字符



      记住使用 \ 前缀


    • 只匹配agai nst较低/大写字符



      尝试使用 aZ \w 而不是 az AZ



      • 检查您匹配的模式是否包含在尝试的模式中



        如果不在此处那么您可能忘记将您的应用程序包含在 INSTALLED_APPS 设置(或应用程序在 INSTALLED_APPS 可能需要查看)




      Django版本



      Django 1.10,删除了通过其python路径反转url的能力。应该使用命名的路径。






      如果您仍然无法追踪问题,请随时提出一个新问题,包括您尝试过的内容,您所研究的内容(您可以链接到此问题),然后将相关代码包含在该问题中 - 您匹配的网址,任何相关的网址格式,错误消息的一部分,显示了django尝试匹配的内容,以及可能的 INSTALLED_APPS 设置(如果适用)。


      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?

      解决方案

      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.

      • NoReverseMatch at /my_url/

        This is the url that is currently being rendered, it is this url that your application is currently trying to access but it contains a url that cannot be matched

      • Reverse for 'my_url_name'

        This is the name of the url that it cannot find

      • with arguments '()' and

        These are the non-keyword arguments its providing to the url

      • keyword arguments '{}' not found.

        These are the keyword arguments its providing to the url

      • n pattern(s) tried: []

        These are the patterns that it was able to find in your urls.py files that it tried to match against

      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

      • Are there any typos?
      • Have you provided the url you're trying to access the given name?
      • If you included the app with a namespace (e.g. include('myapp.urls', namespace='myapp'), then you need to include the namespace when reversing, e.g. {% url 'myapp:my_url_name' %} or reverse('myapp:my_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:

      • The value is missing or an empty string

        This generally means that the value you're passing in doesn't contain the value you expect it to be. Take a look where you assign the value for it, set breakpoints, and you'll need to figure out why this value doesn't get passed through correctly.

      • The keyword argument has a typo

        Correct this either in the url pattern, or in the url you're constructing.

      If they are correct:

      • Debug the regex

        You can use a website such as regexr to quickly test whether your pattern matches the url you think you're creating, Copy the url pattern into the regex field at the top, and then use the text area to include any urls that you think it should match against.

        Common Mistakes:

        • Matching against the . wild card character or any other regex characters

          Remember to escape the specific characters with a \ prefix

        • Only matching against lower/upper case characters

          Try using either a-Z or \w instead of a-z or A-Z

      • Check that pattern you're matching is included within the patterns tried

        If it isn't here then its possible that you have forgotten to include your app within the INSTALLED_APPS setting (or the ordering of the apps within INSTALLED_APPS may need looking at)

      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.

      这篇关于什么是NoReverseMatch错误,我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆