django在基于类的视图(ListView)

时间:2016-11-22 12:11:58

标签: python django django-forms django-class-based-views

我有以下基于类的视图,我想用它来呈现一个formset,并在通过post方法提交时验证它:

formset呈现完美。当我提交表单时,我可以阅读formset并检查它是否有错误。在这个类的post方法中 - > errors = backorder_formset.errors

如果我在formset中发现任何错误,我想渲染视图,但这次是使用formset实例,我从POST读取。 当我在类的post方法中调用ctx = self.get_context_data()表单时,会从调用super(MissingProductsListView, self).get_context_data(*args, **kwargs)中引发以下错误: 的 'MissingProductsListView' object has no attribute 'object_list'

似乎Listview的超类执行此调用:queryset = kwargs.pop('object_list', self.object_list)

我的问题是为什么我在运行此错误?我如何使用其错误消息呈现此formset以在发布后在模板中显示它?我正在使用Django 1.9.9

class MissingProductsListView(generic.ListView):
    template_name = 'dashboard/purchaseorder/missing_products.html'
    context_object_name = 'backorders'
    model = BackOrder

    def post(self, request, *args, **kwargs):
        backorder_formset = BackOrderFormset(request.POST)
        errors = backorder_formset.errors

        if backorder_formset.is_valid():
            # <process form cleaned data>
            return HttpResponseRedirect('/success/')
        else:
            ctx = self.get_context_data()
            return self.render_to_response(ctx)


    def accumulate_identical_products_from_backorders(self, back_order_list):
        ... some code
        return sorted_accumulated_dict.values()

    def get_context_data(self, *args, **kwargs):
        ctx = super(MissingProductsListView, self).get_context_data(*args, **kwargs)
        ctx['title'] = _("Missing Products")
        if self.request.POST:
            ctx['back_order_formset'] = BackOrderFormset(self.request.POST)
        else:
            accumulated_backorders_per_product = self.accumulate_identical_products_from_backorders(BackOrder.objects.all())

            back_orders = BackOrderFormset(initial=[{'product_id': backorder_dict['product_id'],
                                                     'product': backorder_dict['title'],
                                                     'quantity': backorder_dict['quantity']} for backorder_dict in
                                                    accumulated_backorders_per_product])
            ctx['back_order_formset'] = back_orders
        return ctx

    def get_queryset(self):
      .. some code
        return backorder_list

1 个答案:

答案 0 :(得分:1)

看这里:

class BaseListView(MultipleObjectMixin, View):
    """
    A base view for displaying a list of objects.
    """
    def get(self, request, *args, **kwargs):
        self.object_list = self.get_queryset()
        allow_empty = self.get_allow_empty()

        if not allow_empty:
            # When pagination is enabled and object_list is a queryset,
            # it's better to do a cheap query than to load the unpaginated
            # queryset in memory.
            if self.get_paginate_by(self.object_list) is not None and hasattr(self.object_list, 'exists'):
                is_empty = not self.object_list.exists()
            else:
                is_empty = len(self.object_list) == 0
            if is_empty:
                raise Http404(_("Empty list and '%(class_name)s.allow_empty' is False.") % {
                    'class_name': self.__class__.__name__,
                })
        context = self.get_context_data()
        return self.render_to_response(context)

基本上 - 你错过了POST处理程序中的这一部分:

self.object_list = self.get_queryset()

说实话 - 我不太确定在django中将帖子添加到通用ListView是否是一个好主意。它看起来更像是FormView - 但我在这里错了。