Django - Mixing ListView and CreateView

后端 未结 4 1290
无人共我
无人共我 2020-12-14 11:55

I\'m want to create one page with a form, and every time I submit the form it adds an item to the list below the form.

I can make it work using 2 pages:

4条回答
  •  感动是毒
    2020-12-14 12:12

    I use a lot of views that involve a form and a list of objects. Rather than trying to mixin things I just add the queryset into the context data as below.

    class UploadFileView(CreateView):
        form_class = UploadFileForm
        success_url = 'listview'
        template_name = 'textfrompdf/index.html'
    
        def get_context_data(self, **kwargs):
            kwargs['object_list'] = PdfFile.objects.order_by('id')
            return super(UploadFileView, self).get_context_data(**kwargs)
    

提交回复
热议问题