2014-03-02 48 views
0

我通過GET請求獲得表單顯示信息,目前我在get_context_data中分配了一個新表單,有沒有更好的方法來實現它?而http修飾器也不起作用。如何在通過GET獲取時使用模型數據填充formview

@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

使用UpdateView代替FormViewSingleObjectMixin。在99%的情況下,您不應該重寫get方法。

相關問題