我尝试在可能的应用程序中使用烧瓶登录:
我的控制器:
@app.route("/process_log", methods=['POST'])
def process_login():
filled_form = LoginForm(request.form)
if filled_form.validate():
phone = filled_form.phone.data
password = filled_form.password.data
if User.phone_exists(phone) and User.pass_match(phone, password):
user = User.get_by_phone(phone)
login_user(user.get_id)
return redirect(url_for("index"))
else:
return render_template("login.html", form = filled_form, error = u"Не верный логин или пароль")
else:
return render_template("home.html", form = filled_form)
我有一些定义了烧瓶登录API所需的函数的类
我的用户类:
from pymongo import MongoClient
from bson.objectid import ObjectId
class User():
client = MongoClient()
db = client['test']
col = db['user']
user_id = None
def __init__(self, dic):
self.dic = dic
def is_authenticated():
return True
def is_anonymous():
return False
def is_active():
return True
def get_id(self):
return unicode(str(self.user_id))
def save(self):
self.user_id = self.col.insert(self.dic)
print "Debug:" + str(self.user_id)
@staticmethod
def _get_col():
client = MongoClient()
db = client['test']
col = db['user']
return col
@staticmethod
def phone_exists(phone):
col = User._get_col()
if col.find_one({'phone': phone}) != None:
return True
else:
return False
@staticmethod
def pass_match(phone, password):
col = User._get_col()
if col.find_one({'phone': phone})['password'] == password:
return True
else:
return False
@staticmethod
def get(userid):
col = User._get_col()
return col.find_one({'_id':userid})
@staticmethod
def get_by_phone(phone):
col = User._get_col()
dic = col.find_one({'phone': phone})
print dic['password']
return User(dic)
如您所见,函数 is_active 已定义(注意:我也尝试使用 self 传递引用)
但我仍然有这个错误AttributeError: 'function' object has no attribute 'is_active'
我很抱歉这里有太多代码,但它应该非常简单。
注意:我在我的项目中使用 mongodb。
请帮我找出我的错误。太感谢你了
还有一件事: 我应该为 login_user(....) 提供 Id 还是我的用户对象?