Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I am using Flask.

I am doing an ajax post and I need to check for the existence of a key

I tried the following, but it didn't work

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

What would be the right way to do that?

like image 600
Tampa Avatar asked Sep 17 '12 08:09

Tampa


1 Answers

Your example works fine in python 2.x code

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
like image 173
Jon Clements Avatar answered Oct 14 '22 19:10

Jon Clements