I have two forms named GoodAtForm
and PaidForForm
. What these do is as follows...
GoodAtForm
Takes an input from a list in request.session['love']
and presents it to the user.
Then user is presented with a CheckboXSelectMultiple
fields so that users can select.
After The form is submitted in the view, the user choices are then stored inside another list 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'
This is my 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
)
View For the 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})
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)