0

I'm making a page where the user selects elements, confirms his choice, the data is then sent to the backend, and a .csv is made. After the file is created I want the user to be redirected to a page where he can download the file, however

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

doesn't redirect the user to that page, or any other. There isn't any error in the console, the file is created, but the is no redirection to the page. Do you have any idea of what could cause this ?

@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)

Santeau
  • 839
  • 3
  • 13
  • 23

1 Answers1

1

The reason this does not work is because your browser is submitting a POST request in order to submit the form to your Flask app and as a result is not expecting a new web page to be rendered back to it.

You could try returning a redirect() instead (I haven't tested this myself, but from the docs), e.g.

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)

Alternatively, client side you should submit the POST request and once that has completed successfully, make a GET request to ask for the new page.

Ian

Ian Ash
  • 1,087
  • 11
  • 23
  • Related stackoverflow post: https://stackoverflow.com/questions/14343812/redirecting-to-url-in-flask – Ian Ash Aug 06 '20 at 07:41
  • Thank you for your response, I understand better the issue. sadly, redirect didn't fix my problem, I'll try to make a get request – Santeau Aug 06 '20 at 23:46