后端烧瓶在查询后无法重定向

时间:2020-08-06 06:37:27

标签: flask python-3.6

我正在创建一个页面,用户可以在其中选择元素,确认选择,然后将数据发送到后端,并创建一个.csv。 创建文件后,我希望将用户重定向到他可以下载文件的页面,但是

return render_template("tools/downloadfile.html", document=name)

不会将用户重定向到该页面或任何其他页面。 控制台中没有任何错误,文件已创建,但是没有重定向到页面。 您是否知道可能导致这种情况的原因?

@app.route('/createdocument', methods=['POST', 'GET'])
#@login_required
def create_document():
    playlists = get_playlists()
    if request.method == "POST": 
        request_data = str(request.data.decode('UTF-8'))
        genre = get_header_genre(request_data)
        parsed_data = parse_request(request_data)           
        playlist_names = get_parsed_playlists(parsed_data)
        if genre == "playlist":
            #make_playlist_doc(playlist_names, genre)
            print("playlist option not ready yet")
        elif genre == "socan":
            name = make_socan_doc(playlist_names, genre)
            return render_template("tools/downloadfile.html", document=name)
        else:
            print("other request:")
            print(str(request.data.decode('UTF-8')))
    return render_template("tools/createdocument.html", playlists=playlists)

1 个答案:

答案 0 :(得分:1)

之所以不起作用,是因为您的浏览器正在提交POST请求,以便将表单提交到Flask应用,因此,不希望新的网页呈现回给它。

您可以尝试返回redirect()(例如,我自己没有测试过,而是从docs回来),例如

def create_document():
    playlists = get_playlists()
    if request.method == "POST": 
        # code removed
        if genre == "playlist":
            #make_playlist_doc(playlist_names, genre)
            print("playlist option not ready yet")
        elif genre == "socan":
            name = make_socan_doc(playlist_names, genre)
            return redirect("http://www.example.com", code=302)
    return render_template("tools/createdocument.html", playlists=playlists)

或者,客户端应提交POST请求,一旦成功完成,请发出GET请求以请求新页面。

伊恩

相关问题