gpt4 book ai didi

flask - 无法像 flask-peewee 的教程那样成功

转载 作者:行者123 更新时间:2023-12-03 16:04:22 33 4
gpt4 key购买 nike

我学习使用 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')

直到这一部分:

We now have a functioning admin site! Of course, we’ll need a user log in with, so open up an interactive python shell in the directory alongside the app and run the following:

在本教程中,我们在 python shell 中执行此操作(我知道我们在添加用户之后执行此操作并以手动方式传递):

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

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

谢谢。

最佳答案

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

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

关于flask - 无法像 flask-peewee 的教程那样成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17347779/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com