我正在尝试将Flask-Login添加到我的Flask-Python应用程序中,但是我遇到了这个错误,我不知道为什么。 我想那是我的user.loader不正确但想法是根据ID或电子邮件加载用户。
知道我做错了什么吗?
提前致谢
此致
完整错误:
DataError:(psycopg2.DataError)整数的输入语法无效: “t@t.pt”第3行:WHERE users.uid ='t@t.pt'
app.py
from flask import Flask, render_template, request, session, redirect, url_for
from models import db, User
from forms import SignupForm, LoginForm
from flask_login import LoginManager, UserMixin, \
login_required, login_user, logout_user
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://localhost/polex'
db.init_app(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login"
app.secret_key = "development-key"
@login_manager.user_loader
def user_loader(uid):
"""Given *user_id*, return the associated User object.
:param unicode user_id: user_id (email) user to retrieve
"""
return User.query.get(uid)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/signup", methods=["GET", "POST"])
def signup():
if 'email' in session:
return redirect(url_for('home'))
form = SignupForm()
if request.method == "POST":
if form.validate() == False:
return render_template('signup.html', form=form)
else:
newuser = User(form.first_name.data, form.last_name.data, form.email.data, form.password.data)
db.session.add(newuser)
db.session.commit()
session['email'] = newuser.email
return redirect(url_for('home'))
elif request.method == "GET":
return render_template('signup.html', form=form)
@app.route("/login", methods=["GET", "POST"])
def login():
print db
form = LoginForm()
if request.method == "POST":
if form.validate() == False:
return render_template("login.html", form=form)
else:
email = form.email.data
password = form.password.data
user = User.query.filter_by(email=email).first()
if user is not None and user.check_password(password):
login_user(user, remember=True)
return redirect(url_for('home'))
else:
return redirect(url_for('login'))
elif request.method == 'GET':
return render_template('login.html', form=form)
@app.route("/logout")
def logout():
session.pop('email', None)
return redirect(url_for('index'))
@app.route("/home", methods=["GET", "POST"])
def home():
return render_template("home.html")
if __name__ == "__main__":
app.run(debug=True)
models.py 来自flask_sqlalchemy导入SQLAlchemy 来自werkzeug import generate_password_hash,check_password_hash 来自flask_login导入UserMixin
db = SQLAlchemy()
class User(db.Model):
__tablename__ = 'users'
uid = db.Column(db.Integer, primary_key = True)
firstname = db.Column(db.String(100))
lastname = db.Column(db.String(100))
email = db.Column(db.String(120), unique=True)
pwdhash = db.Column(db.String(54))
#-----login requirements-----
def is_active(self):
#all users are active
return True
def get_id(self):
# returns the user e-mail. not sure who calls this
return self.email
def is_authenticated(self):
return self.authenticated
def is_anonymous(self):
# False as we do not support annonymity
return False
def __init__(self, firstname, lastname, email, password):
self.firstname = firstname.title()
self.lastname = lastname.title()
self.email = email.lower()
self.set_password(password)
def set_password(self, password):
self.pwdhash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.pwdhash, password)
答案 0 :(得分:0)
您正在使用User.query.get(uid)
,您需要使用filter
或filter_by
:
@login_manager.user_loader
def user_loader(user_email):
"""Given *user_id*, return the associated User object.
:param unicode user_id: user_id (email) user to retrieve
"""
return User.query.filter_by(email=user_email).first()
get()
接受一个主键(在这种情况下是用户ID)并返回它,如果你试图限制一个列值,那么filter就是你需要的。