Reputation: 824
I have a form that I am processing in django. It is processing the form correctly but after the processing is done, it is throwing an http response error which could not possibly be happening...
here is the form:
# create a new expense form - group
class CreateExpenseForm(forms.ModelForm):
split_choices = (('1', 'even'),
('2', 'individual'))
split = forms.TypedChoiceField(
choices=split_choices
)
class Meta:
model = Expense
fields = ['location', 'description', 'amount', 'split']
here is the view : I am going to break where the error is happening:
if request.method == 'POST':
form = CreateExpenseForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
location = cd['location']
description = cd['description']
amount = cd['amount']
split = cd['split']
reference = generate_number()
for member in members:
if member.user.username in request.POST:
new_expense = Expense.objects.create(
user = member.user,
group = group,
location = location,
description = description,
amount = amount,
reference = reference,
created_by = user,
)
print('all exepense accounted for')
'all expenses accounted for' is printing in the terminal then it throws the error
now with the form, there are only two choices that split can be which are 1 and 2
I am selecting option 1 (even) so the split should be one, but none of the print message are displaying...
if split == 1:
print('even split')
print(groupid)
print(groupname)
return redirect('even_expense', groupid = groupid, groupname = groupname)
if split == 2:
print('individual splut')
return redirect('test')
it is not printing any error so the http response error has to happen in the section above.
else:
print(form.errors)
return redirect('test')
else:
form = CreateExpenseForm()
parameters = {
'form':form,
'members':members,
}
return render(request, 'groups/create_expense.html', parameters)
Upvotes: 0
Views: 35
Reputation: 1641
Please try this:
field = forms.TypedChoiceField(choices=CHOICES, coerce=int)
Adding coerce=int will convert the value to the type you want, here the type is int.
Upvotes: 1