2017-09-27 54 views
0

瓶子模板擴展因某些原因不起作用。瓶子模板擴展問題

我創建具有以下文件結構的基本燒瓶應用程式:
enter image description here

__init__.py是空

app.py的代碼是:

from flask import Flask, render_template 

app = Flask(__name__) 

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

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

index.html該代碼是:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <meta http-equiv="X-UA-Compatible" content="ie=edge"> 
    <title>Document</title> 
</head> 
<body> 

    <h1>from index</h1> 

    {% block ext %} 
    {% endblock %} 

</body> 
</html> 

ext.html的代碼是:

{% extends "index.html" %} 

{% block ext %} 

    <h2>from ext</h2> 

{% endblock %} 

當我在命令行python app.py運行它沒有給出錯誤或警告,日誌只是

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 
127.0.0.1 - - [27/Sep/2017 14:48:40] "GET/HTTP/1.1" 200 - 

但在瀏覽器中,只有輸出I有「來自指數」。所以Flask沒有從ext.html中看到模板擴展,我在瀏覽器中看不到「from ext」。這裏有什麼問題?某處有錯字嗎?

回答

1

你正在渲染錯誤的東西。遺傳從孩子到父母;如果你想要使用子模板,那麼這就是你需要調用的。

def index(): 
    return render_template("ext.html")