Home > OS >  Redirect to other HTML file using Flask
Redirect to other HTML file using Flask

Time:06-08

I have 2 HTML files, one I am using in the beginning, and what I want is that after the user clicks a button and a POST request comes into the code, it should change to another HTML page.

My current code (part of it):

...

@app.route('/test', methods=['POST'])                                
def test():
    output = request.get_json()
    print(output)
    render_template('secondform.html', static_folder='static')

...

But the problem is that this doesn't work...

How would I make it that it would that after a POST request has been detected, it would switch to a completely new HTML page.

Any help appreciated.

CodePudding user response:

you can use a redirect function in flask to do that. You can refer to this answer.

CodePudding user response:

To achieve this simply check the request.method and add logic accordingly for GET and POST :

from flask import request
@app.route('/test', methods=["POST", "GET"])                                
def test():
    if request.method == "POST"
        output = request.get_json()
        print(output)
        return render_template('secondform.html', static_folder='static')
    return render_template('<get_request_template_file>.html', static_folder='static') 
    #above will be executed on GET request and will render <get_request_template_file>.html 
  • Related