我会将整个主应用发布到一个文件中,而我不知道模板或主python文件中的错误
import datetime
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_bcrypt import Bcrypt
from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///.../snap.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'putheretrjurtrandom---secretkey'
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
flask_bcrypt = Bcrypt(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(40),unique=True)
email = db.Column(db.String(255),unique=True)
_password = db.Column(db.String(60))
created_on = db.Column(db.DateTime,default=datetime.datetime.utcnow)
def __repr__(self):
return "< User {!r} >".format(self.username)
def is_authenticated(self):
return True
def is_active(self):
return True
def is_anonymous(self):
return False
def get_id(self):
return unicode(self.id)
@hybrid_property
def password(self):
return self._password
@password.setter
def password(self,password):
self._password = flask_bcrypt.generate_password_hash(password)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
#Forms
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField
from wtforms.validators import DataRequired, Length
class LoginForm(FlaskForm):
username = StringField('username', validators=[DataRequired()])
password = PasswordField('password',validators=[DataRequired(), Length(min=6)])
#views
from flask import flash, render_template, url_for, redirect, g
from flask_login import login_user,logout_user, current_user
@app.route('/login', methods=['GET','POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('listing'))
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).first()
if not user:
flash('No such User exists')
return render_template('login.html',form=form)
if (not flask_bcrypt.check_password_hash(user.password,form.password.data)):
flash("Invalid Password.")
return render_template('login.html',form=form)
login_user(user,remember=True)
flash("you have logged in successfully.")
return redirect(url_for('listing'))
return render_template('login.html', form=form)
@app.route('/logout',methods=['GET'])
def logout():
logout_user(user)
return redirect(url_for('listing'))
@app.route('/hello')
def hello():
return 'Hello your name!!'
当我运行小型应用程序时,尝试登录时出现主要错误,但登录页面上出现错误 如何解决这个问题? 我试图解决并修复模板文件,但没有解决方案?