反向Django通用视图,post_save_redirect;错误“包含的 urlconf 没有任何模式”
我确实看到了另一个标题为“如何使用 django 反转通用视图”和“django 命名网址,通用视图”的问题,但是我的问题有点不同,我不相信它是一个骗局。
代码:
from django.views.generic import list_detail, create_update
from django.core.urlresolvers import reverse
from django.conf.urls.defaults import *
partners_add = {'form_class': FooForm,
'post_save_redirect': reverse('foo-list'),
}
urlpatterns = patterns('',
url(r'^foo/$', list_detail.object_list, foo_list, name='foo-list'),
url(r'^foo/add/$', create_update.create_object, foo_add, name='foo-add'),
)
但是,当我运行代码时,出现错误“包含的 urlconf bar.urls 中没有任何模式”。然后当我将reverse('foo-list')更改为'/bar/foo/'时它就起作用了。但是,如果在模板中调用 {% url foo-list %} 我会得到正确的 url 并且代码可以正常工作。
添加反向也会破坏相同 urlpattern 中的所有 url,并出现相同的错误。
我在 Python 2.6 上运行 Django 1.1
I did see the other question titled 'how to use django reverse a generic view' and 'django named urls, generic views' however my question is a little different and I do not believe it is a dupe.
Code:
from django.views.generic import list_detail, create_update
from django.core.urlresolvers import reverse
from django.conf.urls.defaults import *
partners_add = {'form_class': FooForm,
'post_save_redirect': reverse('foo-list'),
}
urlpatterns = patterns('',
url(r'^foo/
However when I run the code I get the error "The included urlconf bar.urls doesn't have any patterns in it". Then when I change reverse('foo-list') to '/bar/foo/' it works. If however, within the template if i call {% url foo-list %} I get the correct url and the code works.
Adding the reverse will also break all urls within the same urlpatterns with the same error.
I'm running Django 1.1 on Python 2.6
, list_detail.object_list, foo_list, name='foo-list'),
url(r'^foo/add/
However when I run the code I get the error "The included urlconf bar.urls doesn't have any patterns in it". Then when I change reverse('foo-list') to '/bar/foo/' it works. If however, within the template if i call {% url foo-list %} I get the correct url and the code works.
Adding the reverse will also break all urls within the same urlpatterns with the same error.
I'm running Django 1.1 on Python 2.6
, create_update.create_object, foo_add, name='foo-add'),
)
However when I run the code I get the error "The included urlconf bar.urls doesn't have any patterns in it". Then when I change reverse('foo-list') to '/bar/foo/' it works. If however, within the template if i call {% url foo-list %} I get the correct url and the code works.
Adding the reverse will also break all urls within the same urlpatterns with the same error.
I'm running Django 1.1 on Python 2.6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我在这里找到的问题的解决方案:
http://andr.in/2009/11/21/calling -reverse-in-django/
我已粘贴下面的代码片段,以防链接消失:
, 'django.views.generic.simple.redirect_to', {'url':reverse_lazy('comehere')}, name='root') )
Here's a solution to the problem I found here:
http://andr.in/2009/11/21/calling-reverse-in-django/
I have pasted the code snippet below in case that link disappears:
, 'django.views.generic.simple.redirect_to', {'url': reverse_lazy('comehere')}, name='root') )
Django 1.4 Alpha 包含一个函数
reverse_lazy
来帮助解决这个问题。Django 1.4 Alpha includes a function
reverse_lazy
to help with this problem.您有一个拼写错误 -
post_save_redirect
之前没有开头引号。另外,您是否导入了list_detail
和create_update
因为您直接引用模块,而不是作为字符串?已编辑我怀疑问题来自于对
partners_add
字典中的reverse
的调用。我认为这将导致循环依赖,因为 urlconf 现在依赖于导入 urlconf 时尚未定义的属性。尝试删除该调用 - 也许对相关 url 进行硬编码 - 并查看它是否有效。
You have a typo - no opening quote before
post_save_redirect
. Also, have you importedlist_detail
andcreate_update
since you are referring to the modules directly, rather than as strings?Edited I suspect that the problem comes from having a call to
reverse
in thepartners_add
dictionary. I think this will lead to a circular dependency, since the urlconf now depends on attributes which have not yet been defined at the time the urlconf is imported.Try removing that call - perhaps hard-code the relevant url - and see if it works.
一种可行的方法是包装 create_object 函数并使用views.py 中的反向函数。
在 urls.py 中,代码可能如下所示:
在 myapp/views.py 中
One way it would work would be to wrap create_object function and use reverse from the views.py.
In urls.py code could look something like this:
and in myapp/views.py