简体   繁体   English

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

[英]How to populate formview with model data when fetched via GET

I have the form display info via GET request, currently I assign a new form in get_context_data, is there a better way to do it? 我通过GET请求获得了表单显示信息,当前我在get_context_data中分配了一个新表单,是否有更好的方法呢? And http decorator doesn't work too. 和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

Use UpdateView instead of FormView with SingleObjectMixin . 使用UpdateView代替FormViewSingleObjectMixin In 99% of the cases you should not override the get method. 在99%的情况下,您不应覆盖get方法。

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

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