盡量避免使用 JS,所以我想知道是否有辦法通過使用按鈕來獲得重定向?我一直在尋找和嘗試,但找不到有效的解決方案。
結構:
main.py
static/
templates/
index.html
404.html
styles/
index.css
Python代碼:
from flask import Flask, redirect, url_for, render_template
app = Flask(__name__)
@app.route("/")
def home():
return render_template("index.html")
@app.errorhandler(404)
def page_not_found(e):
return render_template("404.html")
app.run(debug=True)
404.html 的 HTML,其中有一個我想重定向回主頁的按鈕
<div class = "btn-bot">
<button class="button" type="button" role="button" href="{{ url_for('home') }}">Return</button>
</div>
單擊按鈕和所有這些東西都可以正常作業,但它從不執行任何操作或重定向。控制臺什么也不輸出。
uj5u.com熱心網友回復:
這很正常,你應該使用標簽a。
<div class = "btn-bot">
<a href="{{ url_for('home') }}">
<button class="button" type="button" role="button">Return</button>
</a>
</div>
我想這很容易理解,不是嗎?
uj5u.com熱心網友回復:
答案是將按鈕線更改為
<a href="/home"><button class="button" type="button" role="button">Return</button></a>
在 Python 檔案中,剛剛添加 /home 作為索引的另一條路徑
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/405475.html
標籤:
