【问题标题】:how to check in flask whether a variable parameter is passed in url or not?如何在烧瓶中检查是否在 url 中传递了可变参数?
【发布时间】:2020-07-16 17:07:27
【问题描述】:

例如网址是:http://localhost:5000/create/user/<int:id>

我要检查用户是否传递了id参数。

我已经做到了。

@app.route('/create/user/<int:id>')
def create_user(id=None):
    if id is None:
         return jsonify({'message':'Bad request'})

例如

  1. http://localhost:5000/create/user/23
  2. http://localhost:5000/create/user/

1 网址中,用户提供了 id=23 的值,因此我们将继续处理,但在 2 网址中,用户没有为 2 提供任何值strong>id 参数,所以我们会抛出某种错误。

我想知道,如何实现这样的功能。

【问题讨论】:

    标签: python flask rest dynamic-url


    【解决方案1】:

    在变量w 中将存储您所需支票的Boolean value

    url = http://localhost:5000/create/user/1234
    id = 1234
    w = str(id) in ''.join(url.split('http://localhost:5000/create/user/'))
    

    【讨论】:

      【解决方案2】:

      您可以使用正则表达式的正面外观来执行此操作:

      (?<=user\/)\d+
      

      这匹配前面有user/ 的任意位数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-13
        • 2016-03-10
        • 2022-07-09
        • 1970-01-01
        • 2016-04-13
        • 1970-01-01
        相关资源
        最近更新 更多