混合ListView和CreateView

混合ListView和CreateView

本文介绍了Django - 混合ListView和CreateView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用表单创建一个页面,每次提交表单时,它都会在表单下面添加一个项目。



我可以使用2个页面使其工作:




  • 使用mixin CreateView 添加一页项目

  • 一页 ListView 列表。






但是我试图将表单和列表放在同一页面上。所以我试图用mixin创建一个类:

  class FormAndListView(ListView,CreateView):
pass

然后我使用了这个类:

  FormAndListView.as_view(
queryset = PdfFile.objects.order_by('id'),
context_object_name ='all_PDF',
success_url ='listview'
form_class = UploadFileForm,
template_name ='textfrompdf / index.html',)),

但是当我尝试加载页面时,我会收到错误:异常值:'FormAndListView'对象没有属性'object'

 追溯:
文件C:\Program Files\Python_2.7\lib\site-packages\django\ core_\\handlers\base.pyin get_response
111. response = callback(request,* callback_args,** callback_kwargs)
文件C:\Program Files\Python_2.7\ lib\site- package \django\views\generic\base.py在视图
47. return self.dispatch(request,* args,** kwargs)
文件C:\Program文件$ pa $ b中的\Python_2.7\lib\site-packages\django\views\generic\base.py68.返回处理程序(request,* args,** kwargs)
文件C:\Program Files\Python_2.7\lib\site-packages\django\views\generic\list.py在get
122. return self.render_to_response (上下文)
在render_to_response中的文件C:\Program Files\Python_2.7\lib\site- packages\django\views\generic\base.py
94 。template = self.get_template_names(),
文件C:\Program Files\Python_2.7\lib\site- packages\django\views\generic\list.py get_template_names
134. names = super(MultipleObjectTemplateResponseMixin,self).get_template_names()
文件C:\Program Files\Python_2.7\lib\site-packages\django\views\generic\detail.py在get_template_names
122.如果self.object和self。 template_name_field:

异常类型:/ PDF /
中的AttributeError异常值:'FormAndListView'对象没有属性'对象'

我不知道如何调试。在哪里开始?

解决方案

我发现答案有两个问题:




  • ListView和CreateView是低级
    级别混合的高级混合。但是这些较低级别的混合不兼容。

  • View类直接调用render_to_response(),但在我的场景中,有2个视图类,render_to_response()只应该被调用一次$ / $>




    我能够解决这个问题,使用以下步骤:



    我使用较低级别的mixins而不是调用ListView和CreateView。另外我调用了显式的BaseCreateView和BaseListView,我从中提取窗体和object_list

      class FormAndListView(BaseCreateView,BaseListView,TemplateResponseMixin) :
    def get(self,request,* args,** kwargs):
    formView = BaseCreateView.get(self,request,* args,** kwargs)
    listView = BaseListView.get (self,request,* args,** kwargs)
    formData = formView.context_data ['form']
    listData = listView.context_data ['object_list']
    return render_to_response('textfrompdf / index.html',{'form':formData,'all_PDF':listData},
    context_instance = RequestContext(request))

    它不干净,但它的作品!


    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:

    • one page using the mixin CreateView to add items
    • one page ListView to have the list.

    But I'm trying to have the form and the list on the same page. So I tried to create a class with both mixin:

    class FormAndListView(ListView, CreateView):
        pass
    

    Then I've used this class:

    FormAndListView.as_view(
        queryset=PdfFile.objects.order_by('id'),
        context_object_name='all_PDF',
        success_url = 'listview',
        form_class = UploadFileForm,
        template_name='textfrompdf/index.html',)),
    

    But when I try to load the page, I get the error: Exception Value: 'FormAndListView' object has no attribute 'object'

    Traceback:
    File "C:\Program Files\Python_2.7\lib\site-packages\django\core\handlers\base.py" in get_response
      111.                         response = callback(request, *callback_args, **callback_kwargs)
    File "C:\Program Files\Python_2.7\lib\site-packages\django\views\generic\base.py" in view
      47.             return self.dispatch(request, *args, **kwargs)
    File "C:\Program Files\Python_2.7\lib\site-packages\django\views\generic\base.py" in dispatch
      68.         return handler(request, *args, **kwargs)
    File "C:\Program Files\Python_2.7\lib\site-packages\django\views\generic\list.py" in get
      122.         return self.render_to_response(context)
    File "C:\Program Files\Python_2.7\lib\site-packages\django\views\generic\base.py" in render_to_response
      94.             template = self.get_template_names(),
    File "C:\Program Files\Python_2.7\lib\site-packages\django\views\generic\list.py" in get_template_names
      134.             names = super(MultipleObjectTemplateResponseMixin, self).get_template_names()
    File "C:\Program Files\Python_2.7\lib\site-packages\django\views\generic\detail.py" in get_template_names
      122.         if self.object and self.template_name_field:
    
    Exception Type: AttributeError at /PDF/
    Exception Value: 'FormAndListView' object has no attribute 'object'
    

    I've no idea how to debug that. Where to start?

    解决方案

    I found the answer, there is 2 problems:

    • ListView and CreateView are "high level" mixin which aggregate "lowerlevel" mixins. But these lower level mixins are not compatible together.
    • The View class calls directly the render_to_response(), but in my scenario, there is 2 view class and render_to_response() should only be called once at the end.

    I was able "solve" this issue using the following steps:

    Instead of calling ListView and CreateView, I used lower level mixins. Moreover I called explicitly BaseCreateView and BaseListView from which I "extracted" the form and object_list

    class FormAndListView(BaseCreateView, BaseListView, TemplateResponseMixin):
        def get(self, request, *args, **kwargs):
            formView = BaseCreateView.get(self, request, *args, **kwargs)
            listView = BaseListView.get(self, request, *args, **kwargs)
            formData = formView.context_data['form']
            listData = listView.context_data['object_list']
            return render_to_response('textfrompdf/index.html', {'form' : formData, 'all_PDF' : listData},
                               context_instance=RequestContext(request))
    

    It's not clean but it works!

    这篇关于Django - 混合ListView和CreateView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 03:28