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

后端 未结 4 1292
再見小時候
再見小時候 2021-02-06 17:01

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

相关标签:
4条回答
  • 2021-02-06 17:36

    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')
    )
    0 讨论(0)
  • 2021-02-06 17:43

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

    0 讨论(0)
  • 2021-02-06 17:48

    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 讨论(0)
  • 2021-02-06 17:49

    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"))
    
    0 讨论(0)
提交回复
热议问题