【发布时间】:2016-11-03 08:36:21
【问题描述】:
from flask import Flask, request
app = Flask(__name__)
@app.route('/post/', methods=['GET', 'POST'])
def update_post():
# show the post with the given id, the id is an integer
postId = request.args.get('postId')
uid = request.args.get('uid')
return postId, uid
def getpostidanduid():
with app.app_context():
credsfromUI = update_post()
app.logger.message("The postId is %s and uid is %s" %(request.args.get('postId'), request.args.get('uid')))
## Prints 'The post id is red and uid is blue'\
return credsfromUI
print(getpostidanduid())
if __name__ == '__main__':
# This is used when running locally. Gunicorn is used to run the
# application on Google App Engine. See entrypoint in app.yaml.
app.run(host='127.0.0.1', port=8080, debug=True)
# [END app]
这是一个程序,它接受浏览器 url 中的两位信息(postId 和 uid),并且应该允许我在程序中引用它们。对于我的简单 python 程序,我无法弄清楚为什么我仍然收到 RuntimeError: Working outside of request context。
这通常意味着您尝试使用需要的功能 一个活动的 HTTP 请求。请参阅有关测试的文档 有关如何避免此问题的信息。
我正在使用 with app.app_context() 但它不会让我在请求中获取变量的内容。我已经阅读了文档并查看了其他解决方案,但仍然卡住了。请帮忙?
【问题讨论】: