今天在学习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:
网友评论