Closed edisontcd closed 4 years ago
How are you setting the admin role?
you mean the models.py file?
I mean if this code runs when you register your user. Add a print there to see if that code runs, and if it doesn't then investigate why the conditional returns False
.
thanks, but where and how to add a print?
right before this line: https://github.com/miguelgrinberg/flasky/blob/20ce2f9c5d5a50692673094179deaee1776749cf/app/models.py#L90.
Maybe you should start from the beginning and describe the steps that you are taking. I suspect I'm not really understanding what you are doing.
Thanks for your patience, i just learning part 9 and part 10 of the book 《Flask Web Development》- User Roles and User Profiles.
All the code running in local was copied from https://github.com/miguelgrinberg/flasky/tree/10d, except the config.py, i've added the real info.
All email address can only registered as default_role = 'User'
,include the email configured in FLASKY_ADMIN = os.environ.get('FLASKY_ADMIN', 'xxx@xx.com')
.
The configured email xxx@xx.com
can't registered as an administrator.
Maybe just as u say, i'll try to add a print before self.role = Role.query.filter_by(name='Administrator').first()
, if the code not run, then check the code why the conditional returns False
.
@edisontcd have you register this xxx@xx.com user before getting to the roles feature? If you did, then you have to delete the user from the database and register again.
if self.email == current_app.config['FLASKY_ADMIN']:
print('hello world!')
self.role = Role.query.filter_by(name='Administrator').first()
the print('hello world!') can run successfully as below, and after delete the user from the database, i register again, but still can't registered as an administrator.
127.0.0.1 - - [09/Jun/2020 22:40:18] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [09/Jun/2020 22:40:23] "GET /auth/login HTTP/1.1" 200 -
127.0.0.1 - - [09/Jun/2020 22:40:25] "GET /auth/register HTTP/1.1" 200 -
hello world!
127.0.0.1 - - [09/Jun/2020 22:40:40] "POST /auth/register HTTP/1.1" 302 -
127.0.0.1 - - [09/Jun/2020 22:40:40] "GET /auth/login HTTP/1.1" 200 -
class Config:
SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard to guess string'
MAIL_SERVER = os.environ.get('MAIL_SERVER', 'smtp.xx.com')
MAIL_PORT = int(os.environ.get('MAIL_PORT', '25'))
MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS', 'true').lower() in \ ['true', 'on', '1']
MAIL_USERNAME = os.environ.get('MAIL_USERNAME', 'xxx@xx.com')
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD', 'abcdefg')
FLASKY_MAIL_SUBJECT_PREFIX = '[Flasky]'
FLASKY_MAIL_SENDER = 'Flasky Admin <xxx@xx.com>'
FLASKY_ADMIN = os.environ.get('FLASKY_ADMIN', 'xxx@xx.com')
SQLALCHEMY_TRACK_MODIFICATIONS = False
it's my config code, any problems?
Do you have a role with the name Administrator
stored in your database?
sqlite> select * from roles;
sqlite>
the 'roles' table return nothing
Okay, so you missed the part where the roles are inserted. Have you run the insert_roles() function?
how to run this function? just put in models.py, or need to use in other file?
Are you following the book? This repository is supposed to be used alongside the book.
sqlite> select * from roles;
1|User|1|7
2|Moderator|0|15
3|Administrator|0|31
finally,ok now!!!
before register, have to run this code below:
from app.models import Role
Role.insert_roles()
after that, i have to read your book more carefully, According to my thinking, just run flasky.py is ok, no need to run other codes, Can it be possible when running flasky.py , the roles inserting to the database automatic?
@edisontcd as I said, this project comes with the book. There is a deployment chapter in the book that covers how to deploy the application and get those roles inserted.
OK, thank you very much, it's very kind of you! As a beginner, maybe I'll disturb you more in the future about Flask Web Development, or maybe about python. Do not feel I'm annoying, hahaha~
the config is like this: FLASKY_ADMIN = os.environ.get('FLASKY_ADMIN', 'xxx@xx.com')
but the database is as below, can't register as an admin sqlite> select * from users; 1|xxx@xx.com|edison||pbkdf2:sha256:150000$UtxENiUL$f12c1e1878e3485723134eb557141195600304782c3729d9a242556b380c8f85|1|edison|China|a designer|2020-06-01 13:41:24.615439|2020-06-08 13:10:09.860055|6929bb4579f331bdfef41d5835807a12