gpt4 book ai didi

python - flask : href link to html not working

转载 作者:行者123 更新时间:2023-12-05 02:18:21 24 4
gpt4 key购买 nike

我有一个具有以下结构的基本 Flask 应用程序:

from flask import Flask
from flask import render_template
app = Flask(__name__,template_folder='E:\Programming\Python Projects\Flask')

@app.route('/')
def index():
return render_template('hello.html')
@app.route('/route/')
def route1():
return render_template('route1.html')
app.run(debug = True,port = 8080,host = '0.0.0.0')

你好.html:

<!DOCTYPE html>
<html>
<head>
<title>Rendered!!</title>
</head>
<body>
<h1>
The template has been rendered!!!<br>
<a href="localhost:8080/route">Route No. 1</a>
</h1>
</body>
</html>

route1.html :

<!DOCTYPE html>
<html>
<head>
<title>Route No. 1</title>
</head>
<body>
<h2>
This is the first route!!!<br>
Hello World!!!
</h2>
<iframe src="https://www.youtube.com/embed/YQHsXMglC9A" width="853" height="480" frameborder="0" allowfullscreen></iframe>
</body>
</html>

当我打开 localhost:8080 时,它工作正常。但是当我点击链接时,它说:

The address wasn’t understood
Firefox doesn’t know how to open this address, because one of the following protocols (localhost) isn’t associated with any program or is not allowed in this context.

当我在地址栏中手动输入地址 localhost:8080/route 时,它工作正常。此外,在新选项卡中打开时它工作正常。我需要帮助!!!谢谢!!!

最佳答案

你应该使用from flask import render_template, url_for

在模板中:

<h1>
The template has been rendered!!!<br>
<a href="{{ url_for('route1') }}">Route No. 1</a>
</h1>

只需让 Flask 和 Jinja2 为您制作 URL...

*您似乎忘记了链接处的结尾斜线。 应该是 localhost:8080/route/ 但是最好使用 url_for,因为它可以避免此类问题

关于python - flask : href link to html not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45528007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com