Flask AppRoute中的多个参数 [英] Multiple parameters in Flask approute

查看:151
本文介绍了Flask AppRoute中的多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果URL调用中有多个参数,如何编写烧瓶 app.route ?

How to write the flask app.route if I have multiple parameters in the URL call?

这是我从AJax拨打的URL:

Here is my URL I am calling from AJax:

http://0.0.0.0:8888/createcm?summary=VVV&change=Feauure

我试图像这样写我的烧瓶 app.route :

I was trying to write my flask app.route like this:

@app.route('/test/<summary,change>', methods=['GET']

但是这不起作用.谁能建议我如何提及 app.route ?

But this is not working. Can anyone suggest me how to mention the app.route?

推荐答案

如果您确实想使用查询参数,则其他答案具有正确的解决方案.像这样:

The other answers have the correct solution if you indeed want to use query params. Something like:

@app.route('/createcm')
def createcm():
   summary  = request.args.get('summary', None)
   change  = request.args.get('change', None)

一些注意事项.如果只需要支持GET请求,则无需在路由装饰器中包括这些方法.

A few notes. If you only need to support GET requests, no need to include the methods in your route decorator.

解释查询参数.一切都超出了?"在您的示例中称为查询参数.Flask将从URL中删除那些查询参数,并将它们放入ImmutableDict中.您可以通过 request.args 来访问它,或者使用键,即 request.args ['summary'] 或使用我和其他一些注释者提到的get方法.如果不存在默认值,则可以为它提供默认值(例如无").这对于查询参数很常见,因为它们通常是可选的.

To explain the query params. Everything beyond the "?" in your example is called a query param. Flask will take those query params out of the URL and place them into an ImmutableDict. You can access it by request.args, either with the key, ie request.args['summary'] or with the get method I and some other commenters have mentioned. This gives you the added ability to give it a default value (such as None), in the event it is not present. This is common for query params since they are often optional.

现在,在您的示例中您似乎正在尝试执行另一个选择,那就是使用路径参数.看起来像这样:

Now there is another option which you seemingly were attempting to do in your example and that is to use a Path Param. This would look like:

@app.route('/createcm/<summary>/<change>')
def createcm(summary=None, change=None):
    ...

此处的网址为: http://0.0.0.0:8888/createcm/VVV/Feauure

将VVV和Feauure作为变量传递到您的函数中.

With VVV and Feauure being passed into your function as variables.

希望有帮助.

这篇关于Flask AppRoute中的多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆