我正在尝试使用flask编写登录应用程序。
我的View.py看起来像
@lm.user_loader
def load_user(id):
if id is None or id == 'None':
id =-1
print 'ID: %s, leaving load_user' % (id)
return Users.query.get(int(id))
和另一种登录方法是
@app.route('/login',methods=['GET','POST'])
def login():
form = LoginForm()
if request.method == 'GET':
return render_template('login.html',
title = 'Sign In',\
form = form)
username = form.username
password = form.password
user = Users(nickname = form.username, email = 'sanjeev_yadav248@yahoo.com', role =
ROLE_USER)
if user is None:
flash('Username or Password is invalid' , 'error')
return redirect(url_for('login'))
remember_me = False
if 'remember_me' in session:
remember_me = session['remember_me']
session.pop('remember_me', None)
login_user(user)
flash("Logged in successfully.")
return redirect(request.args.get("next") or url_for("index"))
我的模型(model.py)类是
class Users(db.Model):
id = db.Column(db.Integer, primary_key = True)
nickname = db.Column(db.String(64), unique = True)
email = db.Column(db.String(120), index = True, unique = True)
role = db.Column(db.SmallInteger, default = ROLE_USER)
posts = db.relationship('Post', backref = 'author', lazy = 'dynamic')
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)
def __repr__(self):
return '<User %r>' % (self.nickname)
我的databsase用户表有id,昵称,emailid和角色。 这个昵称将sanjeev作为值和相关的id(例如3)。但是当我运行它时,不会填充load_user()方法id。即它是没有的#39;当我在load_user方法中对id进行硬编码时,它的工作正常。例如,如果我放回Users.query.get(3)。如果你们在这方面有任何线索,请告诉我。