我正在尝试将Flask-Login添加到我的Flask-Python应用程序中,但是出现此错误,我不知道为什么。
我猜那是我的user.loader,它是不正确的,但想法是根据ID或电子邮件来加载用户。
知道我在做什么错吗?
提前致谢
问候
完全错误:
DataError:(psycopg2.DataError)整数的无效输入语法:
“ [email protected]”第3行:WHERE users.uid ='[email protected]'
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导入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)
最佳答案
您在需要使用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。关于python - DataError:(psycopg2.DataError)整数的无效输入语法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40295584/