不能像烧瓶 - 小便的教程那样成功

时间:2013-06-27 15:53:52

标签: flask peewee

我使用flask-peewee学习。与教程there中一样,我应用此脚本(app.py):

import datetime
from flask import Flask
from flask_peewee.auth import Auth
from flask_peewee.db import Database
from peewee import *
from flask_peewee.admin import Admin



# configure our database
DATABASE = {
    'name': 'exampleappusi.db',
    'engine': 'peewee.SqliteDatabase',
}
DEBUG = True
SECRET_KEY = 'ssshhhh'

app = Flask(__name__)
app.config.from_object(__name__)

# instantiate the db wrapper
db = Database(app)


class Note(db.Model):
    message = TextField()
    created = DateTimeField(default=datetime.datetime.now)


# create an Auth object for use with our flask app and database wrapper
auth = Auth(app, db)

admin = Admin(app, auth)
admin.register(Note)

admin.setup()

if __name__ == '__main__':
    auth.User.create_table(fail_silently=True)
    Note.create_table(fail_silently=True)

    app.run(host='0.0.0.0')

直到这部分:

  

我们现在有一个正常运作的管理网站!当然,我们需要用户登录,因此在应用程序旁边的目录中打开一个交互式python shell并运行以下命令:

在教程中,我们在python shell中做(我知道我们这样做是为了添加用户并以手动方式传递):

>> from auth import User
>> admin = User(username='admin', admin=True, active=True)
>> admin.set_password('admin')
>> admin.save()

问题是我从auth import User执行“>>”时出错,这意味着没有名为auth的模块。当然,在这种情况下我们需要auth.py,但是auth.py应该是什么?

感谢。

2 个答案:

答案 0 :(得分:3)

您的模块名为app,因此您应该在那里导入auth

>> from app import auth
>> User = auth.User
>> admin = User(username='admin', admin=True, active=True)
>> admin.set_password('admin')
>> admin.save()

答案 1 :(得分:0)

您可以创建auth.py文件:

auth.py(与app.py相同的目录)

from flask_peewee.auth import Auth
from app import app, db

auth = Auth(app, db)

您还需要一个models.py来创建用户:

https://github.com/coleifer/flask-peewee/blob/master/example/models.py

相关问题