I have a Flask view that accepts a url parameter old_del
, which can only accept 3 values: 'comma', 'pipe', and 'tab'.
@app.route('/jobs/change-file-del/<str:file_location>/<str:old_del>')
def process_feed(file_location, old_del='tab'):
...
I want to return an error if the user includes an invalid value for old_del
. I can accomplish this using an assert
statement, but is there a way to do this specifically with Flask?
There is a built-in any
URL converter. Use that to specify the valid values. If it doesn't match, you'll get a 404.
@app.route('/jobs/change/<str:name>/<any(comma, pipe, tab):delim>')
def process_feed(name, delim='tab'):
pass
If you want to do a more complex check, you can write your own converter instead.
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