如何获取flask中get请求的参数值? [英] How to obtain values of parameters of get request in flask?

查看:68
本文介绍了如何获取flask中get请求的参数值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上找到的答案是使用 request.args.get.但是,我无法让它工作.我有以下简单示例:

The answer that I found on the web is to use request.args.get. However, I cannot manage it to work. I have the following simple example:

from flask import Flask
app = Flask(__name__)

@app.route("/hello")
def hello():
    print request.args['x']
    return "Hello World!"

if __name__ == "__main__":
    app.run()

我在浏览器中转到 127.0.0.1:5000/hello?x=2 结果我得到:

I go to the 127.0.0.1:5000/hello?x=2 in my browser and as a result I get:

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

我做错了什么?

推荐答案

简单的答案是你没有从flask包中导入request全局对象.

The simple answer is you have not imported the request global object from the flask package.

from flask import Flask, request

这很容易通过在调试模式下运行开发服务器来确定

This is easy to determine yourself by running the development server in debug mode by doing

app.run(debug=True)

这会给你一个堆栈跟踪,包括:

This will give you a stacktrace including:

print request.args['x']
NameError: global name 'request' is not defined

这篇关于如何获取flask中get请求的参数值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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