烧瓶在GAE上重定向 [英] Flask redirects on GAE

查看:581
本文介绍了烧瓶在GAE上重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在Google应用引擎上使用了Flask(http://flask.pocoo.org/)。我有一个下面的代码:

  @ app.route(/ edit.html,methods = ['GET','POST '))
def create():$ b $ if request.method =='GET':
form = ImageForm()
return render_template('edit.html',title =如果request.method =='POST':
image = Image()
form = ImageForm(request.form,image)
if form.validate()和request.files ['image']:
form.populate_obj(image)
如果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)$ b $ form = ImageForm(request.form,image)
返回render_template( edit.html',title = u'Edit',form = form)

重定向到一个给定的URL



$ $ p $ return redirect(url_for(edit,id = image.key()))

我收到一条消息:


图片状态:302 FOUND内容类型:
text / html; charset = utf-8位置:
http:// localhost:8080 / edit / agtyb3VnaC1kcmFmdHILCxIFSW1hZ2UYDQw.html
Content-Length:299




重定向...

重定向...

您应该自动将
重定向到
网址:/edit/agtyb3VnaC1kcmFmdHILCxIFSW1hZ2UYDQw.html。
如果没有点击链接。

我不明白我的代码有什么问题吗?

解决方案

在你的代码中的东西是输出文本的响应之前,Flask框架输出其响应(它看起来像它正在打印'图像') - 很可能你的代码中有一个打印语句。结果,标题瓶试图输出get解释为响应正文的一部分。


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: 299

Redirecting...

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?

解决方案

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.

这篇关于烧瓶在GAE上重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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