Checking POST Variables are set?

1,920 views
Skip to first unread message

StevenC

unread,
Aug 5, 2009, 4:14:58 AM8/5/09
to Django users
Hey all!

I need some support with something, i have the following boolean field
but if you submit the form without choosing an answer it kicks back an
error because i have the following IF Statment to make sure when it is
selected other fields are mandatory. HEres the code.


Lived_Out_Uk = forms.TypedChoiceField(choices=choices,
widget=forms.RadioSelect, coerce=int)

if request.method == 'POST':
if request.POST['Lived_Out_Uk'] == '1':
Country_Residence = forms.CharField(required=True)
Date_Reentry = forms.CharField(required=True)
else:
Country_Residence = forms.CharField(required=False)
Date_Reentry = forms.CharField(required=False)

But, when Yes or No is not selected the following error is created:

MultiValueDictKeyError at /application/
Request Method: POST
Request URL: http://webapps.stamford.ac.uk/application/
Exception Type: MultiValueDictKeyError
Exception Value: Key 'Lived_Out_Uk' not found in <QueryDict:
{u'Level_5': .........

I know the error is because the page is not submitting the POST
variable for Lived_Out_Uk and my IF statment is checking if it is set.
So how can i check to see if metho.POST['Lived_Out_Uk'] is set?

krylatij

unread,
Aug 5, 2009, 4:21:12 AM8/5/09
to Django users
You need to ensure that key exists:

if request.POST.get('Lived_Out_Uk', '0') == '1':
.....

Wayne Koorts

unread,
Aug 5, 2009, 4:22:03 AM8/5/09
to django...@googlegroups.com
Hi,

> I need some support with something, i have the following boolean field
> but if you submit the form without choosing an answer it kicks back an
> error because i have the following IF Statment to make sure when it is
> selected other fields are mandatory. HEres the code.

Instead of this line:

if request.POST['Lived_Out_Uk'] == '1':

Use this:

if 'Lived_Out_Uk' in request.POST:

Regards,
Wayne Koorts
http://www.wkoorts.com

StevenC

unread,
Aug 5, 2009, 4:38:54 AM8/5/09
to Django users
Cheers!

Would this work? Im so used ot PHP, i keep thinking in it.


if request.metho == 'POST' && 'Lived_Out_Uk' in request.POST:

Would that work?

Masklinn

unread,
Aug 5, 2009, 4:39:41 AM8/5/09
to django...@googlegroups.com
On 5 Aug 2009, at 10:14 , StevenC wrote:
> I know the error is because the page is not submitting the POST
> variable for Lived_Out_Uk and my IF statment is checking if it is set.
> So how can i check to see if metho.POST['Lived_Out_Uk'] is set?
>
I'm not sure I understand your code samples: are you using a
TypedChoiceField without a form around it (or is that a separate chunk
of code in your forms.py)? And then manually checking for the value?
And if it's a boolean field, why aren't you just using a
forms.BooleanField in a Django form?

Also, look up the `in` operator for future references. But not for
this case, because I think you're doing it wrong.

Finally, please do read PEP8. Python is not (as far as I know) Ada.
The variable names Lived_Out_Uk, Country_Residence and Date_Reentry
don't conform to most Python style guides, they should be all-
lowercase and underscore-separated words, not titlecased.

StevenC

unread,
Aug 5, 2009, 4:41:18 AM8/5/09
to Django users
Masklinn,

Yeah sorry mate, its a really old scirpting habit from previous codes.

Note taken,

All lower case :D

Masklinn

unread,
Aug 5, 2009, 5:36:23 AM8/5/09
to django...@googlegroups.com
On 5 Aug 2009, at 10:38 , StevenC wrote:
> Cheers!
>
> Would this work? Im so used ot PHP, i keep thinking in it.
>
>
> if request.metho == 'POST' && 'Lived_Out_Uk' in request.POST:
>
> Would that work?
>
It wouldn't, because "&&" is not a python operator. The boolean
operator in Python is "and", so `request.method == 'POST' and
'Lived_Out_Uk' in request.POST` will ensure the request has been
POSTed and there's a value for the key 'Lived_Out_Uk' in request.POST.

But really, you should use django's forms for that kind of things.

Also, please read the Python tutorial at least, it's not a big
document and it'll help you a lot.

Reply all
Reply to author
Forward
0 new messages