访问表单中的 request.session['key'] 时出错.[使用 CheckboxSelectMultiple] [英] Error while accessing request.session['key'] inside forms. [using CheckboxSelectMultiple]
问题描述
我有两个名为 GoodAtForm
和 PaidForForm
的表单.它们的作用如下...
I have two forms named GoodAtForm
and PaidForForm
. What these do is as follows...
GoodAtForm
从request.session['love']
中的列表中获取输入并将其呈现给用户.
GoodAtForm
Takes an input from a list inrequest.session['love']
and presents it to the user.
然后向用户显示一个 CheckboXSelectMultiple
字段,以便用户可以选择.
Then user is presented with a CheckboXSelectMultiple
fields so that users can select.
在视图中提交表单后,用户选择将存储在另一个列表request.session['good']
中.
After The form is submitted in the view, the user choices are then stored inside another list request.session['good']
.
4.另一个名为 PaidForForm
的表单使用该列表进一步询问用户使用 CheckBocSelectMultiple
的问题,并且选择来自列表 ```request.session['好的'].
4.Another Form named PaidForForm
uses that list for further asking of questions from users using CheckBocSelectMultiple
and the selections are from the list ```request.session['good'].
我的问题是我无法访问 Forms 中的输出数据以供查看.
My problem is that I am unable to access output data inside the Forms to provide it to view.
输入在初始化时工作正常.我的表单呈现给定 LOVE 列表中的复选框,但问题是表单不提供输出.它说
Input is working fine when initialised. My forms renders Check Boxes from the given LOVE list but the problem is that Form is not providing output. It says
form = GoodAtForm(request.POST)
input_list = request.session['love']
'QueryDict' object has no attribute 'session'
这是我的GoodAtForm
class GoodAtForm(forms.Form):
def __init__(self, request, *args, **kwargs):
super(GoodAtForm, self).__init__(*args, **kwargs)
input_list = request.session['love']
self.fields['good'] = forms.MultipleChoiceField(
label="Select Things You are Good At",
choices=[(c, c) for c in input_list],
widget=forms.CheckboxSelectMultiple
)
查看 GoodAtForm
def show_good_at(request):
if request.method == 'POST':
form = GoodAtForm(request.POST) #it is showing problem here. Throws an exception here
if form.is_valid():
if not request.session.get('good'):
request.session['good'] = []
request.session['good'] = form.cleaned_data['good']
return redirect('paid_for')
else:
form = GoodAtForm(request=request) #rendering form as usual from the list 'love'
return render(request, 'good_at_form.html', {'form':form})
推荐答案
通常传递给 Django 表单的第一个位置"参数是请求数据,您已经将 request
定义为第一个参数到您的表单类,但在您的视图中传递 request.POST
Usually the first "positional" argument passed to a Django form is the request data, you've defined request
as the first argument to your form class but are passing request.POST
in your view
每次实例化表单时,您都需要将请求作为第一个参数传递
You either need to pass request as the first argument every time that you instantiate your form
form = GoodForm(request, request.POST)
或将请求更改为关键字参数
or change request to be a keyword argument
class GoodAtForm(forms.Form):
def __init__(self, *args, request=None, **kwargs):
super().__init__(*args, **kwargs)
...
form = GoodForm(request.POST, request=request)
这篇关于访问表单中的 request.session['key'] 时出错.[使用 CheckboxSelectMultiple]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!