用户可以在Flask URL参数中使用的限制值 [英] Limit values a user can use in a Flask URL parameter
问题描述
我有一个Flask视图,它接受一个url参数 old_del
,它只能接受3个值:'comma','pipe'和'tab'。
$ b $ pre $
@ app.route('/ jobs / change-file-del /< str:file_location> /< str: old_del>')
def process_feed(file_location,old_del ='tab'):
...
如果用户为 old_del
包含一个无效值,我想返回一个错误。我可以使用 assert
语句来完成这个任务,但是有没有一种方法可以专门用Flask来做到这一点?
有一个内置的 任何
网址转换器。用它来指定有效的值。如果不匹配,你将得到一个404。
@ app.route('/ jobs / change /< ; str:name> /< any(逗号,管道,标签):delim>')
def process_feed(name,delim ='tab'):
pass
如果您想进行更复杂的检查,您可以编写自己的转换器。
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.
这篇关于用户可以在Flask URL参数中使用的限制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!