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

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

问题描述

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

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 个模式:[]

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?

推荐答案

NoReverseMatch 错误表示 Django 无法为您在任何已安装应用程序的 url 中提供的 url 找到匹配的 url 模式.

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.

当无法根据提供的参数识别 URLconf 中的匹配 URL 时,django.core.urlresolvers 会引发 NoReverseMatch 异常.

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.

  • 在/my_url/处无反向匹配

  • 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

反转my_url_name"

Reverse for 'my_url_name'

这是它找不到的网址的名称

This is the name of the url that it cannot find

带有参数()"和

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

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

未找到关键字参数{}".

keyword arguments '{}' not found.

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

These are the keyword arguments its providing to the url

尝试了n个模式:[]

这些是它能够在您的 urls.py 文件中找到并尝试与之匹配的模式

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

首先在源代码中找到与当前呈现的 url 相关的代码 - url、视图和任何涉及的模板.在大多数情况下,这将是您当前正在开发的代码的一部分.

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.

完成此操作后,按照 django 将遵循的顺序通读代码,直到到达试图为您的 my_url_name 构建 url 的代码行.同样,这可能是在您最近更改过的地方.

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.

  • 有错别字吗?
  • 您是否提供了尝试访问给定名称的网址?
  • 如果您在应用程序的 urls.py 中设置了 app_name(例如 app_name = 'my_app'),或者如果您包含具有命名空间的应用程序(例如 include('myapp.urls', namespace='myapp'),那么逆向时需要包含命名空间,例如 {% url 'myapp:my_url_name' %}reverse('myapp:my_url_name').
  • Are there any typos?
  • Have you provided the url you're trying to access the given name?
  • If you have set app_name in the app's urls.py (e.g. app_name = 'my_app') or 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').

参数和关键字参数用于匹配给定 url 中存在的任何捕获组,这些组可以通过 url 模式中的 () 括号标识.

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.

假设您匹配的 url 需要其他参数,请查看错误消息并首先查看给定参数的值是否正确.

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.

如果它们不正确:

  • 值缺失或为空字符串

  • 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.

关键字参数有误

在网址格式或您正在构建的网址中更正此问题.

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

如果它们是正确的:

  • 调试正则表达式

  • Debug the regex

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

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.

常见错误:

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

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

记得用 前缀转义特定字符

Remember to escape the specific characters with a prefix

只匹配小写/大写字符

尝试使用 a-Zw 而不是 a-zA-Z

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

如果它不在这里,那么您可能忘记将您的应用包含在 INSTALLED_APPS 设置中(或者 INSTALLED_APPS 中的应用排序可能需要查看在)

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 1.10 中,删除了通过 Python 路径反转 url 的功能.应该使用命名路径.

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

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

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天全站免登陆