Django:使用Post()方法的ListView? [英] Django: ListView with post() method?
问题描述
表单
的表单(基于 GET
),用于缩小ListView的列表结果和第二个表单 status_form
(根据 POST
)。 因为ListView返回一个项目列表。 表单
允许用户限制选择,并且 status_forms
允许用户通过模态窗体标记不正确的项目(因此需要在同一个模板)。
我的麻烦是 ListView
不附带方法 post
,但是 FormView
。我的类列表
继承自两个类,但是当我执行该类时,我收到错误消息:
属性错误:'List'对象没有属性'status_form'
如何更改我的实现以允许第二个表单通过发布方法
?
class List(PaginationMixin,ListView,FormMixin):
model = ListModel
context_object_name ='list_objects'
template_name ='pages / list.html'
paginate_by = 10#可能每页的项目
def get(self,request,* args,** kwargs):
self.form = ListSearchForm(self.request.GET或者None,)
return super(List,self).get(request,* args,** kwargs)
def post(self,request,* args,** kwargs)
self.status_form = StatusForm(self.request.POST或None)
如果self.status_fo rm.is_valid():
...
else:
return super(List,self).post(request,* args,** kwargs)
def get_queryset(self):
#定义查询
...
#完成后,传递给object_list
return object_list
def get_context_data(self ,** kwargs)
context = super(List,self).get_context_data(** kwargs)
context.update(** kwargs)
上下文['form'] = self。形式
上下文['status_form'] = self.status_form#Django正在抱怨status_form不存在,因为post方法不执行结果
return context
#Django正在抱怨status_form不存在,
#结果,因为post方法不执行
上下文['status_form'] = self.status_form
因为您没有定义 self .status_from
首先。
您已在 get_context_data
中定义,并可从那里访问。
您可以访问您的对象
context = self.get_context_data(*)$ {code> get_context_data
* kwargs)
status_form = context ['status_form']
还要考虑你可以定义您的 status_form
直接在发布
方法本身,而不从 self
或 get_context_data
。
重新设计您的视图以分开单独视图中的每个表单处理,然后用每个其他
重新设计视图:
简而言之,让每个视图执行一项工作。您可以创建一个视图来处理您的 status_form
,并将其命名为 StatusFormProcessView
然后在您的列表
视图返回它的发布
方法
class List(ListView);
def post(self,request,* args,** kwargs):
return StatusFormView.as_view(request)#什么你需要传递给你表单处理视图
这只是一个例子,需要更多的工作才能真实。
另一个例子;在我的网站索引页面上,我有一个搜索表单。当用户 POST
或 GET
搜索表单时,搜索的处理不存在于我的 IndexView
,而是在单独的视图中处理整个表单的东西,如果表单应该在 GET
方法上处理,我将覆盖 get()
方法,如果表单应该在 POST
上处理,我将覆盖 post()
方法将 search_form
数据发送到负责处理 search_form
的视图。 p>
评论回复
status_form = context ['status_form']
不应该是
context ['status_form' ] = status_form
在创建后?
你想从上下文
中获得 status_form
,所以你需要
status_form = context ['status_form']
无论如何,您的表单数据可在 self.request.POST
I am trying to process two forms in a Django class based view. The site contains a form called form
(based on GET
) for narrowing the list results of the ListView and the second form status_form
(based on POST
).
Both forms are required since the ListView returns a list of items. Form
lets the user restrict the choices and status_forms
lets the user flag incorrect items via a modal form (therefore it needs to be in the same template).
My trouble is that ListView
does not come with the method post
, however FormView
does. My class List
inherits from both classes, but when I execute the class I get the error message:
Attribute Error: 'List' object has no attribute 'status_form'
How should I change my implementation to allow the second form been processed via the post method
?
class List(PaginationMixin, ListView, FormMixin):
model = ListModel
context_object_name = 'list_objects'
template_name = 'pages/list.html'
paginate_by = 10 #how may items per page
def get(self, request, *args, **kwargs):
self.form = ListSearchForm(self.request.GET or None,)
return super(List, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.status_form = StatusForm(self.request.POST or None)
if self.status_form.is_valid():
...
else:
return super(List, self).post(request, *args, **kwargs)
def get_queryset(self):
# define the queryset
...
# when done, pass to object_list
return object_list
def get_context_data(self, **kwargs):
context = super(List, self).get_context_data(**kwargs)
context.update(**kwargs)
context['form'] = self.form
context['status_form'] = self.status_form # Django is complaining that status_form is not existing, result since the post method is not executed
return context
# Django is complaining that status_form does not exist,
# result since the post method is not executed
context['status_form'] = self.status_form
Because you didn't define self.status_from
in the first place.
You have defined in get_context_data
, And it's accessible from there.
You can access you object from get_context_data
in your post method;
context = self.get_context_data(**kwargs)
status_form = context['status_form']
Also consider that you can define your status_form
directly in post
method itself without getting it from self
or get_context_data
.
Redesign you views to separate each Form processing in separate Views then tight them with each-other.
Views redesign:
In nutshell, let each view to do one job. You can create a View just for processing your status_form
and name it like StatusFormProcessView
then on your List
view return it on its post
method
class List(ListView);
def post(self, request, *args, **kwargs):
return StatusFormView.as_view(request) # What ever you need be pass to you form processing view
This is just an example of it, need more work to be real.
For another example; On my website index page I have a search form. when user POST
or GET
the search form, The processing of searching doesn't exist in my IndexView
, instead I handle the whole form stuff in separate view, If form should process on GET
method, I'll override get()
method, If form should process on POST
, I'll override post()
method to send search_form
data to the view that is responsible for handling of processing the search_form
.
Comments response
status_form = context['status_form']
shouldn't it be
context['status_form'] = status_form
after I created it ?
You want to get status_form
from context
, So you need to
status_form = context['status_form']
Anyway, your form data are available on self.request.POST
这篇关于Django:使用Post()方法的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!