[英](FLASK) I can't figure out why my html file doesn't update/render
我已經嘗試清除瀏覽器緩存和歷史記錄,但這似乎沒有幫助。 我使用的舊 html 文件(Jinja)似乎沒有更新。
基本上,我在此之前渲染了一個文件,而 jinja 似乎正在渲染那個舊文件(不是樣本或索引),而不是樣本或 index.html。
我是 Flask 的初學者,所以如果這是一個簡單的修復,我很抱歉,我不太了解......
這是我的代碼:
示例.py
from flask import Flask, render_template, redirect, url_for
app = Flask(" APP ")
@app.route("/home")
def home():
return render_template("sample.html")
if __name__ == "__main__":
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.run(debug = True)
index.html (繼承自sample.html)
{% extends "sample.html" %}
{% block title %}Home Page {% endblock %}
{% block content %}
<h1> Welcome to the Index Page!</h1>
{% endblock %}
樣品.html
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Features</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
</li>
</ul>
</div>
</nav>
{% block content %}
{% endblock %}
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
你應該渲染index.html
return render_template("index.html")
而不是simple.html
,作為index.html
正在擴展simple.html
。
你必須像這樣渲染 index.html 文件
def home():
return render_template("index.html") ```
假設將index.html
(而不是模板擴展)傳遞給render_template()
不能解決此問題,這可能取決於您如何啟動應用程序。 如果您通過其他方式啟動它
python sample.py
然后if __name__ == '__main__':
不會執行,並且TEMPLATES_AUTO_RELOAD
不會被設置。 在實例化Flask()
后立即嘗試設置TEMPLATES_AUTO_RELOAD
。 (此外,為了獲得良好的格式,請刪除 APP 兩側的空格。)
確保 html 文件位於模板文件夾中。 我們使用 render_template() 方法來渲染我們將在項目的模板文件夾中創建的 index.html 模板,然后您應該渲染 index.html。
return render_template("index.html")
我剛剛想通了。 我在另一個文本編輯器中運行該文件並且從未結束該過程(即使我關閉了該窗口)。 剛重新啟動我的電腦,問題就解決了。
愚蠢的錯誤,但我猜初學者學習? :D
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.