Flask在GAE上的重定向

1 投票
1 回答
1956 浏览
提问于 2025-04-16 14:32

你好,我正在Google App Engine上使用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)   

但是浏览器没有把我重定向到指定的URL,

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

我收到了一条消息:

状态:302 找到 内容类型: text/html; 字符集=utf-8 位置: http://localhost:8080/edit/agtyb3VnaC1kcmFmdHILCxIFSW1hZ2UYDQw.html 内容长度:299

正在重定向...

正在重定向...

你应该 会自动重定向到目标 URL:/edit/agtyb3VnaC1kcmFmdHILCxIFSW1hZ2UYDQw.html。 如果没有,请点击这个链接。

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

1 个回答

7

你的代码里有东西在Flask框架输出响应之前就已经输出了文本(看起来是打印了'image')。这很可能是因为你在代码的某个地方用了print语句。结果,Flask想要输出的头信息被当成了响应内容的一部分。

撰写回答