如何使用python,flask和命令行传递URL参数 [英] How to pass a URL parameter using python, Flask, and the command line

查看:342
本文介绍了如何使用python,flask和命令行传递URL参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用request.args.get传递URL参数时遇到很多麻烦。

I am having a lot of trouble passing a URL parameter using request.args.get.

我的代码如下:

from flask import Flask, request
app= Flask(__name__)

@app.route('/post?id=post_id', methods=["GET"])
def show_post():
    post_id=1232
    return request.args.get('post_id')

if __name__=="__main__":
     app.run(host='0.0.0.0')

保存后,我总是在命令行中键入python filename.py,请参见运行在 http://0.0.0.0:5000/ (按CTRL + C退出)上作为返回命令行,然后在Chrome中输入网址( http:// ip_addres:5000 / post?id = 1232 )看看它是否会返回1232,但是不会成功!请帮忙。

After saving, I always type python filename.py in the command line, see Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) as the return on the command line, and then type in the url (http://ip_addres:5000/post?id=1232) in chrome to see if it will return 1232, but it won't do it! Please help.

推荐答案

您应该将查询参数排除在路由之外,因为它们不属于该路由。

You should leave the query parameters out of the route as they aren't part of it.

@app.route('/post', methods=['GET'])
def show_post():
    post_id = request.args.get('id')
    return post_id

request.args 是一个包含URL中传递的所有查询参数的字典。

request.args is a dictionary containing all of the query parameters passed in the URL.

这篇关于如何使用python,flask和命令行传递URL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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