![](/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.