jinja2.exceptions.TemplateNotFound: home.html

今天在学习python用户Flask框架搭建一个web服务器遇到了一个问题(jinja2.exceptions.TemplateNotFound: home.html),代码如下:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def home():
  return render_template('home.html')

@app.route('/signin', methods=['GET'])
def signin_form():
  return render_template('form.html')

@app.route('/signin', methods=['POST'])
def signin():
  username = request.form['username']
  password = request.form['password']
  if username == 'admin' and password == 'password':
    return render_template('signin-ok.html', username=username)
  return render_template('form.html', message='Bad username or password', username=username)

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

代码和廖老师教程一样,但在浏览器访问终端就报jinja2.exceptions.TemplateNotFound: home.html,没有找到home.html模版。上网一搜发现Flask读取模版是在app.py下的templates下。

官方文档介绍:

Flask will look for templates in the templates folder. So if your application is a module, this folder is next to that module, if it’s a package it’s actually inside your package:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容