![](/img/trans.png)
[英]Django: How to pre-populate FormView with dynamic (non-model) data?
[英]How to populate formview with model data when fetched via GET
我通過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
使用UpdateView
代替FormView
和SingleObjectMixin
。 在99%的情況下,您不應覆蓋get
方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.