[Django]-Reverse Django generic view, post_save_redirect; error 'included urlconf doesnt have any patterns'

8πŸ‘

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')
)
πŸ‘€jfenwick

2πŸ‘

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

πŸ‘€Duncan Parkes

1πŸ‘

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.

0πŸ‘

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"))
πŸ‘€bmihelac

Leave a comment