I'm not able to find the proper syntax for doing what I want to do. I want to do something if a name/value pair is not present. Here is the code in my view:
if (!request.POST['number']): # do something
What is the proper way to accomplish something like the above? I am getting a syntax error when I try this.
@Thomas gave you the generic way, but there is a shortcut for the particular case of getting a default value when a key does not exist.
number = request.POST.get('number', 0)
This is equivalent to:
if 'number' not in request.POST: number = 0 else: number = request.POST['number']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With