5

Currently, my website has two drop down boxes on it whose values, upon form submission, translate to two URL parameters "myState" and "myShelter". For example:

https://www.atweather.org/forecast?myState=CT&myShelter=181

I'm now adding a third box and there will be a third corresponding parameter called "myTrail". My question is this: how can I make it so that if someone directly submits a URL with just the two parameters, the browser will not error out with a bad request? I would want the user to see the page as though "myState" and "myShelter" were picked, but "myTrail" was just left unselected.

I tried looking here and here, but I don't think these are quite the same situation as what I'm asking about. I'm using Flask under Python 3 and currently handle this route like so:

@app.route('/forecast', methods = ['GET'])
def forecast(loc_state = None, loc_id = None):

    """
    Handles rendering of page after a location has been selected
    """

    loc_state = request.args['myState']
    loc_id = int(request.args['myShelter'])

    ...and a bunch of other logic for rendering the page...

Thanks in advance for any insight!

0

1 Answer 1

4

The request.args.get() method allows to get the argument if it exists.

# return None if the argument doesn't exist
trail = request.args.get('myTrail')

# return 'x' if the argument doesn't exist
trail = request.args.get('myTrail', 'x')

After this, just handle the value to return the way you want.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.