繁体   English   中英

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

[英]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代替FormViewSingleObjectMixin 在99%的情况下,您不应覆盖get方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM