2017-08-06 122 views
0

我有以下結構的基本瓶的應用:瓶: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工作正常。 此外,它在新選項卡中打開時工作正常。 我需要幫助! 謝謝!

+0

你可以嘗試改變鏈接只是'/路由'使它相對而不是絕對?或者,您可以添加'http://'作爲鏈接「http:// localhost:8080/route」的前綴。 – abagshaw

回答

0

您應該使用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,因爲它避免了這種類型的問題