【问题标题】:Linking to another page within a website with Flask使用 Flask 链接到网站中的另一个页面
【发布时间】:2020-02-23 08:40:04
【问题描述】:

我正在尝试制作一个网站,而现在我只是想确保我可以让基本部分正常工作。

当我单击索引页上应将我重定向到测试页的链接时,我收到 404 Not Found 错误。

我进行了大量搜索,试图找出如何解决我的问题,但没有任何效果。一个看起来应该是我的问题的确切解决方法的问题是404 Error when trying to link to another page flask,但在尝试更改我在 test_link.html 中的 href 以匹配我在 flaskApp.py 中的路线或尝试在 test_link 中尝试 url_for 后,我仍然收到错误消息.html。我不确定还能尝试什么。

任何帮助将不胜感激。

flaskApp.py

from flask import Flask, jsonify, redirect, render_template, request, 
url_for

# Configure application
app = Flask(__name__)

@app.route("/")

def get_index():
    return render_template('index.html')

app.route("/test_link")
def test_link():
    return render_template('test_link.html')

if __name__ == "__main__":
    app.run(debug=True)

index.html

<!DOCTYPE html>

<html lang="en">

<head>
    <title>hello</title>
</head>

<body>
    <h1>Index Page</h1>

    <p>
        This is a test <a href="test_link">link</a>
    </p>
</body>

</html>

test_link.html

<!DOCTYPE html>

<html lang="en">

<head>
    <title>hello</title>
</head>

<body>
    test link
</body>

【问题讨论】:

    标签: python html flask http-status-code-404


    【解决方案1】:

    您在app.route("/test_link") 之前错过了@,并且您的路线没有注册。这就是为什么你有404

    【讨论】:

    • 我花了一下午的时间试图弄清楚...感谢您的快速回复
    • 不用感谢,把它标记为正确答案:)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多