Home > Enterprise >  Unable to redirect to another route in Flask
Unable to redirect to another route in Flask

Time:11-05

I am unable to redirect my url to another route.

I want to redirect to /success but it doesn't reach there.

The print statement in app.route("/success") is never implemented and it never returns anything.

I tried a lot of error handling but am unable to figure out the issue.

application.py


app = Flask(__name__)

REGISTRANTS = {}

SPORT=["Cricket", "Football", "Badminton", "Kho-Kho", "Kabaddi"]

@app.route("/", methods = ["GET", "POST"])
def index():
    if request.method == "GET":
        return render_template("index.html", sports = SPORT)
    name = request.form.get("name")
    sport = request.form.get("sport")
    if not name:
        return render_template("failure.html", message="Name not entered")
    if not sport:
        return render_template("failure.html", message="Sport not selected")
    if sport not in SPORT:
        return render_template("failure.html", message="Sport not in list. Don't try to hack our website.")
    if request.method == "POST":
        REGISTRANTS[name]=sport
        print("yes")
        return redirect("/success")

@app.route("/success", methods=["POST"])
def success():
    print("Please work")
    return render_template("success.html", registrants = REGISTRANTS)

index.html

{% extends "layout.html" %}

{% block body %}

    <h1 style="text-align:center;">Register</h1>
    <form action="/" method="post">
        <input name="name" type="text" autocomplete="off" autofocus placeholder="Name">
        <select name="sport">
            <option value="" disabled selected>Sport</option>
            {% for sport in sports %}
                <option value="{{sport}}">{{sport}}</option>
            {% endfor %}
        </select>
        <input type = "submit" value="Register">
    </form>

{% endblock %}

layout.html


<html lang="en">
    <head>
        <meta name ="viewport" content = "initial-scale=1", width="device-width">
        <title>hello</title>
    </head>
    <body>
        {% block body %}{% endblock %}
    </body>
</html>

success.html


{% block body %}

    hello, {{name}}. Thanks for Registering for {{sport}}!

    <h1>Registrants</h1>
    <table style="border: 2px solid black">
        <thead>
            <tr style="border: 2px solid black">
                <th style="border: 2px solid black">Name</th>
                <th style="border: 2px solid black">Sport</th>
            </tr>
        </thead>
        <tbody>
            {% for name in registrants %}
                <tr style="border: 2px solid black">
                    <td style="border: 2px solid black">{{name}}</td>
                    <td style="border: 2px solid black">{{registrants[name]}}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>

{% endblock %}

I am new to Flask. Please guide me.

CodePudding user response:

A redirect request adds an HTTP header and is sent with a status code 302, which causes the browser to send a new GET request to the specified location. This means that the incoming request on the success route is rejected because it only allows the POST method.

I think if you use the get method here it should work.

@app.route("/success")
def success():
    print("Please work")
    return render_template("success.html", registrants = REGISTRANTS)
  • Related