我是flask / python的新手,正在尝试调试其他人的教程。这是他们所有文件的结构:
/flask_project
/app.py
/myprojects
/__pycache__
/__init__.py
/forms.py
/models.py
/base.html
/login.html
/models.html
/register.html
/home.html
但是,当尝试访问第一个必需的html文件-home.html时,它给我一个“找不到烧瓶模板”错误。我尝试将myprojects文件夹重命名为模板,但这没有帮助。 Flask文档也使我感到困惑-我有一个带有代码的 init .py文件,但是我认为app.py本身是一个模块。这是app.py的代码:
# app.py
from myprojects import app, models, db
from flask import render_template, redirect, request, url_for, flash, abort
from flask_login import login_user, login_required, logout_user
from forms import LoginForm, RegistrationForm
@app.route('/')
def home():
return render_template('home.html')
@app.route('/welcome')
@login_required
def welcome_user():
return render_template('welcome_user.html')
@app.route('/logout')
@login_required
def logout():
logout_user()
flash("You logged out!")
return redirect(url_for('home'))
@app.route('/login', methods = ['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(email = form.email.data).first()
if user.check_password(form.password.data) and user is not none:
login_user(user)
flash('Logged in successfully!')
next = request.args.get('next')
if next == None or notnext[0] == '/':
next = url_for('welcome_user')
return redirect(next)
return render_template('login.html', form=form)
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegistrationForm(request.form)
if request.method == "POST" and form.validate():
user = User(email = form.email.data,
username = form.username.data, password = form.password.data)
db.session.add(user)
db.session.commit()
flash("Thanks for registering!")
return redirect(url_for('login'))
return render_template('register.html', form=form)
if __name__ == '__main__':
app.run(debug = True)