Home > Net >  How to link submit button to another page on python Flask framework?
How to link submit button to another page on python Flask framework?

Time:11-11

I am learning how to do web development and am using PythonAnywhere with the Flask framework because I was taught python in school and am most familiar with it. I am making a login page that should redirect to another page after the username and password have been validated and are correct. I am getting a syntax error for the line result = @app.route("/staff_intranet") How do I make the submit button lead to another page? Below is my function for the login page.

def staff_login():
    errors = ""
    if request.method == "POST":
        username = None
        password = None
        try:
            username = string(request.form["username"])
        except:
            errors  = "<p> Enter a valid username. </p>\n".format(request.form["username"])
        try:
            password = string(request.form["password"])
        except:
            errors  = "<p> Enter a valid password. </p>\n".format(request.form["password"])
        if username is not None and password is not None:
            result = @app.route("/staff_intranet")
    return '''
        <html>
            <body>
                {errors}
                <form method = "post" action = ".">
                    <label for "username"> Username: </label>
                    <br>
                    <input type = "text" id = username>
                    <label for "password"> Password </label>
                    <br>
                    <input type = "password" id = pwd>
                    <br><br>
                    <input type="submit" value="Submit">
                </form>
            </body>
        </html>
    '''.format (errors=errors)

CodePudding user response:

Use flask redirect method

from flask import Flask,redirect

def staff_login():
    errors = ""
    if request.method == "POST":
        username = None
        password = None
        try:
            username = string(request.form["username"])
        except:
            errors  = "<p> Enter a valid username. </p>\n".format(request.form["username"])
        try:
            password = string(request.form["password"])
        except:
            errors  = "<p> Enter a valid password. </p>\n".format(request.form["password"])
        if username is not None and password is not None:
            return redirect("/staff_intranet")
    return '''
        <html>
            <body>
                {errors}
                <form method = "post" action = ".">
                    <label for "username"> Username: </label>
                    <br>
                    <input type = "text" id = username>
                    <label for "password"> Password </label>
                    <br>
                    <input type = "password" id = pwd>
                    <br><br>
                    <input type="submit" value="Submit">
                </form>
            </body>
        </html>
    '''.format (errors=errors)
  • Related