Search code examples
javascriptpythonhtmlflaskhttp-post

Redirect to other HTML file using Flask


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.


Solution

  • 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