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

Issue

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?

Solution

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

Answered By – Jon Clements

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published