访问表单内的request

访问表单内的request

本文介绍了访问表单内的request.session ['key']时出错。 [使用CheckboxSelectMultiple]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种名为 GoodAtForm PaidForForm 的表格。这些操作如下...

I have two forms named GoodAtForm and PaidForForm. What these do is as follows...


  1. GoodAtForm request.session ['love'] 中的列表输入,并将其呈现给用户。

  1. GoodAtForm Takes an input from a list in request.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 ['good']列表中进行选择。

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'].

我的问题是我无法访问表单中的输出数据以提供查看。

My problem is that I am unable to access output data inside the Forms to provide it to view.

初始化时输入工作正常。我的表单从给定的爱列表中呈现复选框,但问题是表单未提供输出。它说

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]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 05:03