反向Django通用视图,post_save_redirect;错误“包含的urlconf没有任何模式” [英] Reverse Django generic view, post_save_redirect; error 'included urlconf doesnt have any patterns'

查看:91
本文介绍了反向Django通用视图,post_save_redirect;错误“包含的urlconf没有任何模式”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确实看到了另一个题为如何使用django反向通用视图和 django命名为url,通用视图的问题,但是我的问题有些不同,我不认为这是一个骗子。

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.

代码:

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中没有任何模式。然后,当我将反向( foo-list)更改为 / bar / foo /时,它可以工作。但是,如果在模板中,如果我调用{%url foo-list%},我将获得正确的url,并且代码可以正常工作。

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.

添加相反的内容也会破坏所有url

Adding the reverse will also break all urls within the same urlpatterns with the same error.

我正在python 2.6上运行Django 1.1

I'm running Django 1.1 on Python 2.6

推荐答案

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

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')
)

这篇关于反向Django通用视图,post_save_redirect;错误“包含的urlconf没有任何模式”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆