Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django post checkbox data

I'm using a checkbox form in my template and in my view im trying to check if the box has been checked or not I have the following code in my view:

if request.POST['check'] == True:

but then it throws an error if it is unchecked. How do i check if there is a value 'check' in my post data?

Thanks

like image 674
ellieinphilly Avatar asked Jun 03 '11 05:06

ellieinphilly


1 Answers

The Python docs are your friend:

 if request.POST.get('check', False):
     ...do stuff...

You could also do this (more Python docs):

 if "check" in request.POST:
     ... do stuff...
like image 140
LaundroMat Avatar answered Oct 02 '22 14:10

LaundroMat