我通过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
答案 0 :(得分:0)
使用UpdateView
代替FormView
SingleObjectMixin
。在99%的情况下,您不应该覆盖get
方法。