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

问题描述

我是新来的Django,我努力工作,它的魔力

I'm new to django and I'm trying to work its magic

我想与表单创建一个页面,每次我提交表单时它增加了一个项目以表格下面的列表中。

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.

我可以使它工作用2页:

I can make it work using 2 pages:


  • 使用混入 CreateView的添加项目之一页

  • 一个页面的ListView 来纷纷上榜。

  • 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',)),

但是,当我尝试加载网页,我得到的错误:异常值:'FormAndListView'对象有没有属性'对象'

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?

谢谢!

尼科

推荐答案

我找到了答案,有2个问题:


  • ListView和CreateView的是高层混入其中聚集低
    水平混入。但这些低级的混入不兼容在一起。

  • View类直接调用render_to_response()这个,但在我的情况下,有2视图类和render_to_response()这个应该只在最后一次调用。

我能解决使用以下步骤此问题:

而不是调用ListView和CreateView的,我用较低的水平混入。而且我明确地叫,并BaseCreateView从BaseListView我提取的形式和object_list中

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