Flask-Peewee和用户创建不起作用

时间:2014-05-21 18:20:42

标签: python-2.7 sqlite flask peewee flask-peewee

我目前正在处理网站的管理界面。但是,当我测试添加新用户时,我得到以下回溯:

Traceback (most recent call last):
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask/app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask/app.py", line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask_peewee/auth.py", line 170, in login
    form.password.data,
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask_peewee/auth.py", line 128, in authenticate
    if not user.check_password(password):
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask_peewee/auth.py", line 25, in check_password
    return check_password(password, self.password)
  File "/home/cody/Documents/Flask/SUPRacing/flask/lib/python2.7/site-packages/flask_peewee/utils.py", line 138, in check_password
    salt, hsh = enc_password.split('$', 1)
ValueError: need more than 1 value to unpack

以下是我的计划的各个相关领域的代码: model.py

class User(db.Model):
    id = IntegerField(primary_key=True)
    username = CharField()
    password = CharField()
    email = CharField(120)
    admin = BooleanField(default=False)
    active = BooleanField(default=True)

    # Flask-Login integration
    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return self.id

    # Required for administrative interface
    def __unicode__(self):
        return self.username
    def __repr__(self):
        return '<User %r>' % (self.username)

app.py

class UserView(ModelAdmin):
    columns = ('username', 'email', 'admin', 'active')
    filter_exclude = ('id', 'password')
    exclude = ('id')

我确定我错过了一些明显的东西,但我想我会发布它,以防我不是。如果我是,我在这里做错了什么?

以下是管理界面在我这样做时的样子截图。 screenshot from 2014-05-21 12 04 05 screenshot from 2014-05-21 12 04 20 screenshot from 2014-05-21 12 04 44

1 个答案:

答案 0 :(得分:0)

根据Flask-Pewee documentation,如果您定义了自己的自定义用户模型,则必须扩展BaseUser类。这将确保您根据this code

将密码创建为(salt,hash)元组

现在,您收到此行的错误

salt, hsh = enc_password.split('$', 1)

试图拆分从未存在的元组

因此,简短的回答是您应该将用户模型定义为:

class User(db.Model, BaseUser):