Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django最佳实践-如何以Django的方式创建一个包含表单和项目列表的页面?_Django_Forms_Listview_Mixins - Fatal编程技术网

Django最佳实践-如何以Django的方式创建一个包含表单和项目列表的页面?

Django最佳实践-如何以Django的方式创建一个包含表单和项目列表的页面?,django,forms,listview,mixins,Django,Forms,Listview,Mixins,我从django(和stackoverflow!) 我一直在尝试创建一个包含表单和项目列表的网页。( ). 我想出了一个解决方案,但我不相信我的代码 我正在使用Django mixinBaseCreateView和BaseListView生成表单和列表上下文数据。但是因为它是视图,所以它们直接调用render\u to\u response() 因此,我重载了get()方法来手动调用两个父方法并提取上下文数据。然后我自己调用了render\u to\u response() class Form

我从django(和stackoverflow!)

我一直在尝试创建一个包含表单和项目列表的网页。( ). 我想出了一个解决方案,但我不相信我的代码

我正在使用Django mixin
BaseCreateView
BaseListView
生成表单和列表上下文数据。但是因为它是视图,所以它们直接调用
render\u to\u response()

因此,我重载了
get()
方法来手动调用两个父方法并提取上下文数据。然后我自己调用了
render\u to\u response()

class FormAndListView(BaseCreateView, BaseListView, TemplateResponseMixin):
    def get(self, request, *args, **kwargs):
        formView = BaseCreateView.get(self, request, *args, **kwargs) # formView contains a response_class
        listView = BaseListView.get(self, request, *args, **kwargs)   # listView contains a response_class as well...
        formData = formView.context_data['form']                      # extract the form from the response_class 
        listData = listView.context_data['object_list']               # extract the object list from the response_class

        return render_to_response('textfrompdf/index.html', {'form' : formData, 'all_PDF' : listData},
                           context_instance=RequestContext(request))
一方面,我没有重新编写mixin中已经存在的内容来管理表单和项目列表。。。另一方面,django正在计算整个
render\u to\u response()
3次


编写此页面的干净Django方法是什么?

如果您刚开始使用Django,请使用普通的旧函数进行查看。在使用新的基于类的视图之前,先弄清楚它是如何工作的。这返回“FormAndListView”对象是不可编辑的异常。
class FormAndListView(BaseCreateView, BaseListView, TemplateResponseMixin):

    def render_to_response(context, **responsekwargs)
        return context

    def get(self, request, *args, **kwargs):
        context = {}
        context.update( BaseCreateView.get(self, request, *args, **kwargs) ) # formView contains a response_class
        context.update( BaseListView.get(self, request, *args, **kwargs) )  # listView contains a response_class as well...

        return TemplateResponseMixin.render_to_response('textfrompdf/index.html', context,
                           context_instance=RequestContext(request))