【问题标题】:jinja2.exceptions.TemplateNotFound error when making a python virtual environment制作python虚拟环境时jinja2.exceptions.TemplateNotFound错误
【发布时间】:2021-11-01 18:48:43
【问题描述】:

所以我使用 Flask 创建了一个网站,直到今天我尝试创建一个 python 虚拟环境时它工作得非常好。有谁知道可能发生了什么?这是我的 python 文件的代码。

from flask import Flask, render_template

app = Flask(__name__, template_folder='html_scripts')

@app.route('/home/')
def home():
    return render_template('html_scripts')

@app.route('/about/')
def about():
    return render_template('html_scripts')

if __name__ == '__main__':
    app.run(debug = True)

这是我的主 html 文件的代码:

<!DOCTYPE html>
<html>
  <head>
      <title>Flask App</title>
      <link rel="stylesheet" href="{{url_for('static',filename='css/main.css')}}">
  </head>
  <body>
    <header>
      <div class="container">
        <h1 class="logo">Adrian's web app</h1>
        <strong><nav>
          <ul class="menu">
            <li><a href="{{ url_for('home') }}">Home</a></li>
            <li><a href="{{ url_for('about') }}">About</a></li>
          </ul>
        </nav></strong>
      </div>
    </header>
    <div class="container">
        {%block content%}
        {%endblock%}
    </div>
  </body>
</html>

这些是剩下的 2 个文件(home_page.html 和 about.html):

{%extends 'layout.html'%}
{%block content%}
<div class = 'home'>
    <h1>My homepage</h1>
   <p>This is my homepage</p>
</div>
{%endblock%}

和,

{%extends 'layout.html'%}
{%block content%}
<div class = 'about'>
    <h1>My about page</h1>
    <p>This is my about page</p>
</div>
{%endblock%}

如果你知道怎么做,请帮忙,因为我还没有看到有人遇到这个问题,我自己也无法解决。 谢谢!

【问题讨论】:

  • 我们需要更多信息 - 您从哪里得到错误?您尝试访问哪条路线会导致错误?您是否能够成功启动虚拟环境?你是如何调用 App/Flask 的?

标签: python flask virtual-environment


【解决方案1】:

render_template 中,尝试使用实际的html 文件。下面的代码解决了你的问题

from flask import Flask, render_template

app = Flask(__name__, template_folder='html_scripts')

@app.route('/home/')
def home():
    return render_template('/home_page.html')

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

if __name__ == '__main__':
    app.run(debug = True)

【讨论】:

  • 非常感谢!
  • 这成功了
猜你喜欢
  • 2013-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-03
  • 1970-01-01
  • 2018-04-16
  • 2019-03-21
  • 2021-01-05
相关资源
最近更新 更多