Flask 在 GAE 上重定向
您好,我正在 Google 应用引擎上使用 Flask (http://flask.pocoo.org/)。我有以下代码
@app.route("/edit.html", methods=['GET', 'POST'])
def create():
if request.method == 'GET':
form = ImageForm()
return render_template('edit.html', title=u'Add', form=form)
if request.method == 'POST':
image = Image()
form = ImageForm(request.form, image)
if form.validate() and request.files['image']:
form.populate_obj(image)
if request.files['image']:
image.file = request.files['image'].read()
image.put()
return redirect(url_for("edit", id=image.key()))
else:
return render_template('edit.html', title=u'Add', form=form)
@app.route("/edit/<id>.html", methods=['GET', 'POST'])
def edit(id):
image = Image.get(id)
form = ImageForm(request.form, image)
return render_template('edit.html', title=u'Edit', form=form)
时给定的网址
return redirect(url_for("edit", id=image.key()))
,但浏览器没有将我重定向到收到消息
:图像状态:302 FOUND 内容类型: 文本/html;字符集=utf-8 位置:
http://localhost:8080/edit/agtyb3VnaC1kcmFmdHILCxIFSW1hZ2UYDQw.html
内容长度:299正在重定向...
重定向...
您应该 自动重定向到目标 网址:/edit/agtyb3VnaC1kcmFmdHILCxIFSW1hZ2UYDQw.html。 如果没有,请点击链接。
我不明白我的代码有什么问题?
Hi I am using Flask (http://flask.pocoo.org/) on Google app engine. I have a following code
@app.route("/edit.html", methods=['GET', 'POST'])
def create():
if request.method == 'GET':
form = ImageForm()
return render_template('edit.html', title=u'Add', form=form)
if request.method == 'POST':
image = Image()
form = ImageForm(request.form, image)
if form.validate() and request.files['image']:
form.populate_obj(image)
if request.files['image']:
image.file = request.files['image'].read()
image.put()
return redirect(url_for("edit", id=image.key()))
else:
return render_template('edit.html', title=u'Add', form=form)
@app.route("/edit/<id>.html", methods=['GET', 'POST'])
def edit(id):
image = Image.get(id)
form = ImageForm(request.form, image)
return render_template('edit.html', title=u'Edit', form=form)
but browser doesn't redirect me to an a given url in
return redirect(url_for("edit", id=image.key()))
I get a message:
image Status: 302 FOUND Content-Type:
text/html; charset=utf-8 Location:http://localhost:8080/edit/agtyb3VnaC1kcmFmdHILCxIFSW1hZ2UYDQw.html
Content-Length: 299Redirecting...
Redirecting...
You should
be redirected automatically to target
URL: /edit/agtyb3VnaC1kcmFmdHILCxIFSW1hZ2UYDQw.html.
If not click the link.
I can't understand what's fault with my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Flask 框架输出其响应之前,代码中的某些内容正在将文本输出到响应(看起来无论它是什么正在打印“图像”)——很可能您的代码中的某处有一个 print 语句。因此,标头 Flask 会尝试将输出解释为响应正文的一部分。
Something in your code is outputting text to the response before the Flask framework outputs its response (it looks like whatever it is is printing 'image') - most likely you have a print statement somewhere in your code. As a result, the headers flask tries to output get interpreted as part of the body of the response instead.