Search code examples
flaskpeewee

Cannot succes do as in tutorial of flask-peewee


I learning using flask-peewee. As in tutorial there, i apply this script (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')

And until this part:

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:

As an in the tutorial, we do in python shell (I understand that we do this following to adding user and pass as manual way):

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

The problem is i get error when execute ">> from auth import User", which mean No module named auth. Sure, in this case we need auth.py , but what the auth.py should be ?

Thanks.


Solution

  • Your module is named app so you should import auth there.

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