代码之家  ›  专栏  ›  技术社区  ›  BeeNag

在同一模板页面上显示分页ListView和UpdateView

  •  1
  • BeeNag  · 技术社区  · 7 年前

    我正在尝试创建一个 Django 页面中可以更新某些内容,并且可以在分页表中查看某些内容。模型如下所示:

    class CostGroup(models.Model):
        name = models.CharField(max_length=200)
        description = models.CharField(max_length=200)
    
        def get_absolute_url(self):
            return reverse(
                'costgroup_detail', 
                kwargs={
                    'costgroup_pk': self.pk,
                }
            )
    
    
    class Cost(models.Model):
        cost_group = models.ForeignKey(CostGroup)
        amount = models.DecimalField(max_digits=50, decimal_places=2)
    
        def get_absolute_url(self):
            return reverse(
                'cost_detail', 
                kwargs={
                    'cost_pk': self.pk,
                }
            )
    

    因此,编辑表单用于 name description 的字段 CostGroup 模型和表格应显示“金额”列表`

    UpdateView 用于表单模板中包含的表单和表格。现在,由于我想在表中包含分页,我需要在同一页上使用两个视图。我设计的页面最终应该是这样的:

    Wireframe for Django page

    我现在不担心样式,我现在主要关注的是把表格和表格放在同一页上。在当前状态下,我唯一没有的是表的分页:

    当前视图如下所示:

    class CostDetail(UpdateView):
        model = models.Cost
        pk_url_kwarg = 'cost_pk'
        template_name = 'main/cost_detail.html'
        form_class = forms.CostDetailEditForm
        success_url = reverse_lazy('cost_list')
    

    我有一种感觉,利用Django CBV使用的底层混合可能是一条路,但我不确定如何开始。

    任何帮助都将不胜感激

    谢谢你抽出时间

    2 回复  |  直到 7 年前
        1
  •  1
  •   Dashdrum    7 年前

    (作为一个新的答案,这一澄清似乎效果更好)

    看起来你正在处理这两张桌子。对象级别使用成本组,而列表视图显示链接到成本组的成本的子记录。假设这是真的,我会这样做:

    class CostDetail(ModelFormMixin, ListView):
    
        model = CostGroup   # Using the model of the record to be updated
        form_class = YourFormName  # If this isn't declared, get_form_class() will
                                   # generate a model form 
    
    
        ordering =  ['id']
        paginate_by = 10
        template_name = 'main/cost_detail.html' # Must be declared
    
        def get_queryset(self):
            # Set the queryset to use the Cost objects that match the selected CostGroup
            self.queryset = Cost.objects.filter(cost_group = get_object())
            # Use super to add the ordering needed for pagination
            return super(CostDetail,self).get_queryset()
    
        # We want to override get_object to avoid using the redefined get_queryset above
        def get_object(self,queryset=None):  
            queryset = CostGroup.objects.all()
            return super(CostDetail,self).get_object(queryset))
    
        # Include the setting of self.object in get()
        def get(self, request, *args, **kwargs):
    
            # from BaseUpdateView
            self.object = self.get_object()
    
            return super(CostDetail,self).get(request, *args, **kwargs)
    
        # Include the contexts from both
        def get_context_data(self, **kwargs):
    
            context = ModelFormMixin.get_context_data(**kwargs)
            context = ListView.get_context_data(**context)
            return context
    
        # This is the post method found in the Update View
        def post(self, request, *args, **kwargs):
    
            # From BaseUpdateView
            self.object = self.get_object()
    
            # From ProcessFormView
            form = self.get_form()
            self.form = form
            if form.is_valid():
                return self.form_valid(form)
            else:
                return self.form_invalid(form)
    
        def put(self, *args, **kwargs):
            return self.post(*args, **kwargs)
    

    我还没有试着运行这个,所以可能有错误。祝你好运

    (请记住,当深入基于类的视图时,ccbv.co.uk是您的朋友)

        2
  •  0
  •   Dashdrum    7 年前

    我正在开发的一个应用程序使用了类似的方法。我从ListView开始,引入FormMixin,然后从FormView引入post()。

    class LinkListView(FormMixin, ListView):
        model = Link
        ordering =  ['-created_on']
        paginate_by = 10
        template_name = 'links/link_list.html'
    
        form_class = OtherUserInputForm
    
        #=============================================================================#
        #
        #     Handle form input
        #
    
        def post(self, request, *args, **kwargs):
            """
            Handles POST requests, instantiating a form instance with the passed
            POST variables and then checked for validity.
            """
            form = self.get_form()
            self.form = form
            if form.is_valid():
                return self.form_valid(form)
            else:
                return self.form_invalid(form)
    
        def put(self, *args, **kwargs):
            return self.post(*args, **kwargs)
    
        def get_success_url(self):
            return reverse('links')
    

    您可能还希望覆盖 get_object() get_queryset() get_context() .