带有表单的基于Django类的视图ListView

带有表单的基于Django类的视图ListView,第1张

带有表单的基于Django类的视图ListView

这些答案对引导我朝正确的方向大有帮助。谢谢大家

对于我的实现,我需要一个窗体视图,该窗体视图同时在get和post上返回ListView。我不喜欢重复get函数的内容,但需要进行一些更改。现在,self.form也可以从get_queryset获得该表单。

from django.http import Http404from django.utils.translation import ugettext as _from django.views.generic.edit import FormMixinfrom django.views.generic.list import ListViewclass FormListView(FormMixin, ListView):    def get(self, request, *args, **kwargs):        # From ProcessFormMixin        form_class = self.get_form_class()        self.form = self.get_form(form_class)        # From baseListView        self.object_list = self.get_queryset()        allow_empty = self.get_allow_empty()        if not allow_empty and len(self.object_list) == 0: raise Http404(_(u"Empty list and '%(class_name)s.allow_empty' is False.")    % {'class_name': self.__class__.__name__})        context = self.get_context_data(object_list=self.object_list, form=self.form)        return self.render_to_response(context)    def post(self, request, *args, **kwargs):        return self.get(request, *args, **kwargs)class MyListView(FormListView):    form_class = MySearchForm    model = MyModel    # ...


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5017681.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-15
下一篇2022-11-15

发表评论

登录后才能评论

评论列表(0条)

    保存