The included urlconf manager.urls doesn't have any patterns in it

后端 未结 1 1482
伪装坚强ぢ
伪装坚强ぢ 2020-12-16 11:34

A solution: Found the following django snippet that seems to work fine (http://djangosnippets.org/snippets/2445/)

from django.utils.function         


        
相关标签:
1条回答
  • 2020-12-16 12:14

    The problem is that your URLConf hasn't finished loading before your class based view attempts to reverse the URL (AddObjView.success_url). You have two options if you want to continue using reverse in your class based views:

    a) You can create a get_success_url() method to your class and do the reverse from there

    class AddObjView(CreateView):
        form_class = ObjForm
        template_name = 'manager/obj_add.html'
    
        def get_success_url():
            return reverse('manager-personal_objs')
    

    b) If you are running on the trunk/dev version of Django, then you can use reverse_lazy https://docs.djangoproject.com/en/dev/topics/http/urls/#reverse-lazy

    from django.core.urlresolvers import reverse_lazy
    
    class AddObjView(CreateView):
        form_class = ObjForm
        template_name = 'manager/obj_add.html'
        success_url = reverse_lazy('manager-personal_objs')
    

    Option "b" is the preferred method of doing this in future versions of Django.

    0 讨论(0)
提交回复
热议问题