【问题标题】:(FLASK) I can't figure out why my html file doesn't update/render(FLASK) 我不知道为什么我的 html 文件没有更新/呈现
【发布时间】: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 是否会引用之前的缓存?

标签: python web flask jinja2


【解决方案1】:

你应该渲染index.html

return render_template("index.html")

而不是simple.html,因为index.html 扩展了simple.html

【讨论】:

    【解决方案2】:

    你必须像这样渲染 index.html 文件

    def home():
        return render_template("index.html") ```
    

    【讨论】:

      【解决方案3】:

      假设将index.html(而不是模板扩展)传递给render_template() 不能解决此问题,这可能取决于您启动应用程序的方式。如果您通过其他方式启动它

      python sample.py
      

      然后if __name__ == '__main__': 不会执行,TEMPLATES_AUTO_RELOAD 不会被设置。在实例化Flask() 后立即尝试设置TEMPLATES_AUTO_RELOAD。 (另外,为了更好的形式,去掉APP两边的空格。)

      【讨论】:

      • 它仍然没有更新渲染:/我现在迷路了
      • 我刚刚想通了。我在另一个文本编辑器中运行该文件并且从未结束该过程(即使我关闭了该窗口)。刚重新启动我的电脑,问题就解决了。很抱歉浪费您的时间!
      • 它发生了。你不太可能再犯这个错误。
      • @CodingBoy 也许您想在这里回答您的问题(即:确保您没有在另一个终端中运行烧瓶应用程序并且它已正确停止)并将其设置为接受的答案。人们可能看不到您的评论并继续在此处发帖以帮助您解决问题!
      【解决方案4】:

      确保 html 文件位于模板文件夹中。 我们使用 render_template() 方法来渲染我们将在项目的 templates 文件夹 中创建的 index.html 模板,然后您应该渲染 index.html。

      return render_template("index.html")
      

      【讨论】:

        【解决方案5】:

        我刚刚想通了。我在另一个文本编辑器中运行该文件并且从未结束该过程(即使我关闭了该窗口)。刚刚重启了我的电脑,问题就解决了。

        愚蠢的错误,但我猜初学者学习? :D

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-01-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-30
          相关资源
          最近更新 更多