【发布时间】:2020-05-29 18:44:48
【问题描述】:
我已经尝试清除浏览器缓存和历史记录,但这似乎没有帮助。我使用的旧 html 文件(Jinja)似乎没有更新。
基本上,我在此之前渲染了一个文件,而 jinja 似乎正在渲染那个旧文件(不是示例或索引),而不是示例或索引 .html。
我是 Flask 的初学者,所以如果这是一个简单的解决方法,我很抱歉,我不太了解...
这是我的代码:
sample.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 %}
sample.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(主页功能)(而不是sample.html)。试一试,如果它不起作用,请确保您的 sample.html 没有错误,方法是删除块并按原样运行。 -
@Hossein 不能解决问题。我的旧文件仍在缓存中,无法更新。
-
如果 HTML 代码有错误,Flask 是否会引用之前的缓存?