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/$', list_detail.object_list, foo_list, name='foo-list'),
url(r'^foo/add/$', 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
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 1.4 Alpha includes a function reverse_lazy
to help with this problem.
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.
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/$', list_detail.object_list, foo_list, name='foo-list'),
url(r'^foo/add/$','myapp.views.my_create_object', name='foo-add'),
)
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"))
来源:https://stackoverflow.com/questions/1794655/reverse-django-generic-view-post-save-redirect-error-included-urlconf-doesnt