我正在尝试在基于 Django 类的视图中处理两种表单。该站点包含一个名为form
(基于GET
)的表单,用于缩小 ListView 的列表结果和第二个表单status_form
(基于POST
)。
两种形式都是必需的,因为 ListView 返回项目列表。Form
让用户限制选择并status_forms
让用户通过模式表单标记不正确的项目(因此它需要在同一个模板中)。
我的麻烦是ListView
该方法没有附带post
,但是FormView
有。我的类List
继承自两个类,但是当我执行该类时,我收到错误消息:
属性错误:“列表”对象没有属性“status_form”
我应该如何更改我的实现以允许通过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