【问题标题】:Unable to redirect to another route in Flask无法重定向到 Flask 中的另一条路线
【发布时间】:2021-11-04 22:43:34
【问题描述】:

我无法将我的网址重定向到另一条路线。

我想重定向到 /success,但它没有到达那里。

app.route("/success") 中的 print 语句永远不会实现,它永远不会返回任何内容。

我尝试了很多错误处理,但无法找出问题所在。

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 %}

我是 Flask 的新手。请指导我。

【问题讨论】:

  • 为什么路由成功的请求方法是POST?我认为如果您在这里使用 GET 方法,它应该可以工作。
  • @Detlef 我做了更改,但它不会重定向。它停留在索引页面上
  • 您可以使用:return redirect(url_for("success")) 而不是:return redirect("/success"),其中 url_for 使用在装饰器中定义的成功例程的路由。添加这个来导入这个函数:from flask import url_for.

标签: python flask


【解决方案1】:

重定向请求会添加一个 HTTP 标头并以状态码 302 发送,这会导致浏览器向指定位置发送一个新的 GET 请求。这意味着成功路由上的传入请求被拒绝,因为它只允许 POST 方法。

我认为如果你在这里使用 get 方法应该可以工作。

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

【讨论】:

  • 我做到了。它不工作。页面停留在索引处,并且打印语句没有被执行,这意味着它没有被重定向。
  • @NisargKudgunti 它适用于我的测试环境。控制台有输出吗?
  • "GET / HTTP/1.0" 200 - 是的 "POST / HTTP/1.0" 302
  • 嘿!这是工作。实际上我是在 IDE 的预览环境中尝试的。当我在浏览器上运行它时它起作用了。您介意告诉我为什么它可以与 GET 一起使用吗?
  • @NisargKudgunti 您不能将转发与 POST 请求结合使用。一个例外是将相同的请求转发到不同的地址。然后数据将再次发送到指定的地址。在您的情况下,没有必要使用 POST 方法。这是对我回答中描述的补充。享受您的进一步实施。
猜你喜欢
  • 1970-01-01
  • 2018-12-18
  • 2016-11-08
  • 2023-01-18
  • 2019-12-16
  • 2015-08-06
  • 2020-04-10
  • 1970-01-01
  • 2014-12-23
相关资源
最近更新 更多