【问题标题】:How to redirect root url in Flask如何在 Flask 中重定向根 url
【发布时间】:2021-08-27 14:08:14
【问题描述】:

我正在尝试使用 Flask 做语音合成网页。当我合成语音时,它会转到另一个网址。使用按钮我应该返回到根 url(主页)。我试过How to redirect a button to root url in flask?这个话题,但它没有生成根网址,在主机“/?”之后生成。

烧瓶代码

from flask import Flask, render_template, request
import inference

app = Flask(__name__)
app.static_folder = 'static'

@app.route("/")
def home():
    return render_template("index.html")

@app.route("/sentez", methods = ["POST"])
def sentez():
    if request.method == "POST":
        metin = request.form["metin"]
        created_audio = inference.create_model(metin)
    return render_template("index.html", my_audio = created_audio)

if __name__ == "__main__":
    app.run();

HTML 代码

{% if not my_audio %}
    <form action="{{ url_for("sentez") }}" method="POST">   
        <textarea id="textalaniid" class="textalani" name="metin"></textarea>
        <button class="w-100 btn btn-lg btn-primary" type="submit" onclick="myFunction()">Seslendir</button>
    </form>
{% endif %}
{% if my_audio %}
    <form action="{{ url_for("home") }}" method="GET">  
        <textarea class="textalani"></textarea>
        <button class="w-100 btn btn-lg btn-primary" type="submit" onclick="myFunction()">Ana Sayfaya Dön</button>
    </form>
{% endif %}

【问题讨论】:

  • 我认为你应该在双引号中使用单引号,你在myFunction中做什么
  • 双引号适用于“sentez”网址。 myfunction 用于如果 textarea 为空但不能用于第二种形式。
  • 在主机 "/?" 之后做是什么意思。
  • 当我点击第二个表单按钮时,我想返回127.0.0.1:5000,但它返回127.0.0.1:5000
  • 这是因为您使用了忘记请求,这应该不会造成任何问题。它在创造吗?

标签: python flask url redirect root


【解决方案1】:

如何使用:

return redirect(url_for('home'))

而不是

return render_template("index.html")

【讨论】:

    【解决方案2】:

    试试这个:

    def sentez():
        if request.method == "POST":
            metin = request.form["metin"]
            created_audio = inference.create_model(metin)
            return render_template("index.html", my_audio = created_audio)
        return render_template("index.html")
    

    【讨论】:

    • 它并没有改变网址问号的事情。
    • 是否解决了返回之前音频的问题。
    • 它不会解决这个问题,这不是问题,请参见本页的网址。你看到?noredirect=.. 这是因为get请求吗。
    • 不,它有时会返回以前的音频。我同意你的 url 事情不是问题。问题出在其他地方。
    • 你能不能把整个代码给我,让我运行看看是什么问题。
    猜你喜欢
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    • 2016-03-10
    • 2017-06-10
    • 2016-10-13
    • 1970-01-01
    相关资源
    最近更新 更多