瓶:HREF链接到HTML工作不
问题描述:
我有以下结构的基本瓶的应用:瓶:HREF链接到HTML工作不
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')
hello.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>
就让瓶和Jinja2的使URL的你...
*看来你忘了结尾的斜线在链接。 应该是本地主机:8080 /路由/ 但它更好地使用url_for,因为它避免了这种类型的问题
你可以尝试改变链接只是'/路由'使它相对而不是绝对?或者,您可以添加'http://'作为链接“http:// localhost:8080/route”的前缀。 – abagshaw