反向Django通用视图,post_save_redirect;错误“包含的 urlconf 没有任何模式”

发布于 2024-08-12 12:03:48 字数 796 浏览 6 评论 0原文

我确实看到了另一个标题为“如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情泪▽动烟 2024-08-19 12:03:49

这是我在这里找到的问题的解决方案:
http://andr.in/2009/11/21/calling -reverse-in-django/

我已粘贴下面的代码片段,以防链接消失:

from django.conf.urls.defaults import *
从 django.core.urlresolvers 导入反向
从 django.utils.function 导入惰性
从 django.http 导入 HttpResponse

verse_lazy = 惰性(反向,str)

urlpatterns = 模式('',
url(r'^comehere/', lambda 请求: HttpResponse('欢迎!'), name='comehere'),
url(r'^
, '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:

from django.conf.urls.defaults import *
from django.core.urlresolvers import reverse
from django.utils.functional import lazy
from django.http import HttpResponse

reverse_lazy = lazy(reverse, str)

urlpatterns = patterns('',
url(r'^comehere/', lambda request: HttpResponse('Welcome!'), name='comehere'),
url(r'^
, 'django.views.generic.simple.redirect_to', {'url': reverse_lazy('comehere')}, name='root') )
梦在夏天 2024-08-19 12:03:49

Django 1.4 Alpha 包含一个函数 reverse_lazy 来帮助解决这个问题。

Django 1.4 Alpha includes a function reverse_lazy to help with this problem.

爱的十字路口 2024-08-19 12:03:49

您有一个拼写错误 - post_save_redirect 之前没有开头引号。另外,您是否导入了 list_detailcreate_update 因为您直接引用模块,而不是作为字符串?

已编辑我怀疑问题来自于对partners_add字典中的reverse的调用。我认为这将导致循环依赖,因为 urlconf 现在依赖于导入 urlconf 时尚未定义的属性。

尝试删除该调用 - 也许对相关 url 进行硬编码 - 并查看它是否有效。

You have a typo - no opening quote before post_save_redirect. Also, have you imported list_detail and create_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 the partners_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.

小瓶盖 2024-08-19 12:03:49

一种可行的方法是包装 create_object 函数并使用views.py 中的反向函数。

在 urls.py 中,代码可能如下所示:

urlpatterns = patterns('',
  url(r'^foo/

在 myapp/views.py 中

from django.views.generic.create_update import create_object
from feincms.content.application.models import reverse

from forms import FooForm


def my_create_object(request):
    return create_object(request, form_class=FooForm, 
                         post_save_redirect=reverse("foo-list"))
, list_detail.object_list, foo_list, name='foo-list'), url(r'^foo/add/

在 myapp/views.py 中


,'myapp.views.my_create_object', name='foo-add'),
  )

在 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:

urlpatterns = patterns('',
  url(r'^foo/

and in myapp/views.py

from django.views.generic.create_update import create_object
from feincms.content.application.models import reverse

from forms import FooForm


def my_create_object(request):
    return create_object(request, form_class=FooForm, 
                         post_save_redirect=reverse("foo-list"))
, list_detail.object_list, foo_list, name='foo-list'), url(r'^foo/add/

and in myapp/views.py


,'myapp.views.my_create_object', name='foo-add'),
  )

and in myapp/views.py

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文