[英]How do I use django.core.urlresolvers.reverse with a function reference instead of a named URL pattern?
In my urls.py
file, I have: 在我的
urls.py
文件中,我有:
from myapp import views
...
(r'^categories/$', views.categories)
Where categories
is a view function inside myapp/views.py
. 其中
categories
是myapp/views.py
的视图函数。 No other URLconf lines reference views.categories
. 没有其他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). 在单元测试文件中,我尝试使用
django.core.urlresolvers.reverse()
捕获此URL,而不仅仅是复制'/ categories /'(DRY等)。 So, I have: 所以我有:
from django.core.urlresolvers import reverse
from myapp import views
...
url = reverse(views.categories)
When I run my tests, I get a NoReverseMatch
error: 运行测试时,出现
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 reverse
documentation , I shouldn't need to make it a named URL pattern just to use reverse
. 但是从
reverse
文档可以看出,我不必为了使用reverse
就使其成为命名URL模式。
Any ideas what I'm doing wrong? 有什么想法我做错了吗?
Jack M.'s example is nearly correct. 杰克·M(Jack 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"),
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. 伙计们,谢谢您的帮助:您启发了我正确地研究它。
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). 这确实可行,并且您粘贴的所有代码都是正确的并且可以正常工作(我只是将其复制到一个干净的测试/项目应用程序中,并且它毫无问题地反转了URL)。 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.
简化到最基本的基础,直到它起作用,然后再开始增加复杂性,看看它在哪里破裂。
Also, you can do "./manage.py shell" and then interactively import the reverse function and your view function and try the reverse. 另外,您可以执行“ ./manage.py shell”,然后以交互方式导入反向函数和视图函数,然后尝试反向。 That'll remove the test setup as a possible cause.
这将消除测试设置的可能原因。
The reverse function actually uses the "name" of the URL. 反向功能实际上使用URL的“名称”。 This is defined like so:
定义如下:
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”调用反向以获取正确的URL。
Ninja Edit: Here is a link to the django docs on the subject. 忍者编辑: 这是有关该主题的django文档的链接 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.