通用视图中的动态success_url

时间:2019-01-29 15:14:51

标签: django django-models django-forms django-templates django-views

我想使success_url像是动态的,而不是给路径success_url =/company/我要给路径名。这样的示例success_url =superadmin_company_update

 path('update/<int:pk>/', views.CompanyUpdateView.as_view(), name='superadmin_company_update'),



 class CompanyUpdateView(LoginRequiredMixin, generic.UpdateView):
            model = Company
            success_url = '/company/'
            template_name = 'company/company_form.html'
            fields = ['company_name', 'company_description', 'company_email', 
            'company_website', 'company_address', 'company_phone', 'company_status', 
            'company_monthly_payment', 'company_logo']

1 个答案:

答案 0 :(得分:0)

找到了get_sucess_url东西,只需要删除success_url = '/company/'

from django.urls.base import reverse
...
class CompanyUpdateView(LoginRequiredMixin, generic.UpdateView):
       model = Company
       template_name = 'company/company_form.html'
       fields = ['company_name', 'company_description', 'company_email', 
                'company_website', 'company_address', 'company_phone', 'company_status', 
                'company_monthly_payment', 'company_logo']

       def get_success_url(self):
            return reverse('superadmin_company')