如何通过GET获取模型数据时填充formview

时间:2014-03-02 10:28:05

标签: python django django-forms django-class-based-views

我通过GET请求显示表单显示信息,目前我在get_context_data中分配了一个新表单,有没有更好的方法呢?并且http装饰器也不起作用。

@require_http_methods(['GET',])
class UniversityDetail(SingleObjectMixin, FormView):
    model = KBUniversity
    form_class = KBUniversityForm
    template_name = 'universities/form.html'

    def get(self, request, *args, **kwargs):
        self.object = self.get_object()
        return super(UniversityDetail, self).get(self, request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(UniversityDetail, self).get_context_data(**kwargs)
        context['form'] = KBUniversityForm(instance=self.object)
        context['university_id'] = self.object.pk
        return context

1 个答案:

答案 0 :(得分:0)

使用UpdateView代替FormView SingleObjectMixin。在99%的情况下,您不应该覆盖get方法。