python 如何将 django.core.urlresolvers.reverse 与函数引用而不是命名 URL 模式一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/134629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:32:37  来源:igfitidea点击:

How do I use django.core.urlresolvers.reverse with a function reference instead of a named URL pattern?

pythondjango

提问by Paul D. Waite

In my urls.pyfile, I have:

在我的urls.py文件中,我有:

from myapp import views
...
(r'^categories/$', views.categories)

Where categoriesis a view function inside myapp/views.py. No other URLconf lines reference views.categories.

categories里面的视图函数在哪里myapp/views.py。没有其他 URLconf 行参考views.categories

In a unit test file, I'm trying to grab this URL using django.core.urlresolvers.reverse(), instead of just copying '/categories/' (DRY and all that). So, I have:

在单元测试文件中,我试图使用 获取这个 URL django.core.urlresolvers.reverse(),而不是仅仅复制 '/categories/'(DRY 和所有这些)。所以我有:

from django.core.urlresolvers import reverse
from myapp import views
...

url = reverse(views.categories)

When I run my tests, I get a NoReverseMatcherror:

当我运行我的测试时,我收到一个NoReverseMatch错误:

NoReverseMatch: Reverse for '<function categories at 0x1082f30>' with arguments '()' and keyword arguments '{}' not found.

It matches just fine if I make the URL pattern a named pattern, like this:

如果我将 URL 模式设为命名模式,它就会很好地匹配,如下所示:

url(r'^categories/$', views.categories, 'myapp-categories')

And use the pattern name to match it:

并使用模式名称来匹配它:

url = reverse('myapp-categories')

But as far as I can tell from the reversedocumentation, I shouldn't need to make it a named URL pattern just to use reverse.

但据我可以从告诉reverse文档,我不应该需要使它一个名为URL模式只是为了利用reverse

Any ideas what I'm doing wrong?

任何想法我做错了什么?

采纳答案by Paul D. Waite

After futher investigation, turns out it was an issue with how I was importing the views module:

经过进一步调查,事实证明这是我如何导入视图模块的问题:

How do I successfully pass a function reference to Django's reverse() function?

如何成功地将函数引用传递给 Django 的 reverse() 函数?

Thanks for the help though, guys: you inspired me to look at it properly.

感谢您的帮助,伙计们:您激励我正确看待它。

回答by camflan

Hyman M.'s example is nearly correct.

Hyman M. 的例子几乎是正确的。

It needs to be a url function, not a tuple, if you want to use named urls.

如果你想使用命名的 url,它需要是一个 url 函数,而不是一个元组。

url(r'^no_monkeys/$', 'views.noMonkeys', {}, "no-monkeys"),

回答by Carl Meyer

This does work, and all the code that you've pasted is correct and works fine (I just copied it into a clean test/project app and it reversed the URL without any problem). So there's something else going on here that you haven't showed us. Simplify down to the bare-bones basics until it works, then start adding complexity back in and see where it's breaking.

这确实有效,并且您粘贴的所有代码都是正确的并且可以正常工作(我只是将其复制到一个干净的测试/项目应用程序中,并且它反转了 URL,没有任何问题)。所以这里还有一些你没有向我们展示的东西。简化到最基本的基础,直到它工作,然后开始增加复杂性,看看它在哪里中断。

Also, you can do "./manage.py shell" and then interactively import the reverse function and your view function and try the reverse. That'll remove the test setup as a possible cause.

此外,您可以执行“./manage.py shell”,然后以交互方式导入反向函数和视图函数并尝试反向。这将删除测试设置作为可能的原因。

回答by Hyman M.

The reverse function actually uses the "name" of the URL. This is defined like so:

reverse 函数实际上使用了 URL 的“名称”。这是这样定义的:

urlpatterns = patterns('',
    (r'^no_monkeys/$', 'views.noMonkeys', {}, "no-monkeys"),
    (r'^admin/(.*)', admin.site.root),
)

Now you would call reverse with the string "no-monkeys" to get the correct url.

现在,您将使用字符串“no-monkeys”调用 reverse 以获取正确的 url。

Ninja Edit: Here is a linkto the django docs on the subject.

Ninja Edit: 这是有关该主题的 django 文档的链接