Bisasam
Bisasam

Reputation: 33

Flask - get redirected to new page after POST-request

I have an edit-button on my Site, that will do a POST-Request and then I want to be redirected to new.html, that contains a form, that holds my POST data.

The POST request in my js-file:

async function onEditBtn() {
    // find correct entry in schedule
    const data = schedule.find(meeting => meeting.id == rowIdOfContextMenu);
    rowIdOfContextMenu = -1;

    const response = await fetch("editmeeting", {
        method: 'POST',
        body: data});
}

The receiving Flask-route:

@app.route('/editmeeting', methods=['POST'])
def edit():
    data = request.get_json()
    print(data)
    return render_template('new.html', data)

POST request is succesful, but I am not getting redirected to new.html.

If I use window.location.href = "<route to my edit page>"; or window.location.replace(); on js side using the response I get from the POST request, my edit page won't get the POST-data.

EDIT: What I was looking for was window.location.href with POST instead of GET.

I ended up using Flask's session object. Did a POST request to /editmeeting, saved the POST-data in a session object, then manually redirected my page to another route rendering new.html using window.location.href in my js-code, and used the data stored in the session.

Thank you for your aswers!

Upvotes: 1

Views: 2819

Answers (1)

gittert
gittert

Reputation: 1308

You could add GET method to this function

@app.route('/editmeeting', methods=['GET', 'POST']

or add this to your imports

from flask import Flask, redirect, url_for

and change the return of the function to something like this (only works when 'new' is indeed a function in your app.

return redirect(url_for('new'))

Upvotes: 2

Related Questions