5

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?

1
  • what returns the view, a specific template or an ajax response ? why not display a message if old_del not in { 'comma', 'pipe', 'tab'}: ... Commented Oct 29, 2017 at 19:07

1 Answer 1

9

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.