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

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

问题描述

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

  from flask import Flask 
app = Flask(__ name__)

@ app.route(/ hello)
def hello():
打印request.args ['x']
返回Hello World!

if __name__ ==__main__:
app.run()

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

 内部服务器错误

服务器遇到内部错误,无法完成请求。服务器过载或者应用程序出现错误。

我做错了什么?

解决方案

最简单的答案是您没有从flask包中导入 request 全局对象。

  from flask import Flask,请求

通过在调试模式下运行开发服务器,很容易确定你自己。

  app.run(debug = True)

这会给你一个stacktrace包括:

<$ p $打印请求.args ['x']
NameError:未定义全局名称'request'


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()

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.

What am I doing wrong?

解决方案

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

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

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