Checking for the existence of a key in request.args in Flask


Question

I am using Flask.

I am doing a ajax post and I need to check for existence if keys.

I tried this but did not work.

if request.args.has_key('campaign_id_crid'):
        print True

What is a better way to test?

1
13
9/17/2012 8:57:07 AM

Accepted Answer

Your example works fine (once an if is added to the start anyway...)

Anyway, although dict.has_key is still about (in existing 2.x code - but removed in Python 3), it's generally considered more Pythonic to use the in operator such as:

if 'campaign_id_crid' in request.args:
    pass # do something
30
9/17/2012 8:58:31 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon