Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
Ricardo Sierra14,920 Points
HELP! Flask @Login_manager.user_loader NOT WORKING!
In a project based in the social media site of Kenneth Love, I am having trouble in the user_loader. When I run it the following problem appears.
File "C:\Users\pc\Documents\S4V5\paperapp\routes.py", line 27, in load_user return models.User.select().where(models.User.id == int(user_id)).get() ValueError: invalid literal for int() with base 10: 'None'
My database is an existing MYSQL schema, with the table User in it.
What am I doing wrong?
It could be the fact that you are creating a Primary key and Peewee also creates a primary key by default when the database is created. Delete the database, remove the uid creation and change the view. See models.py below
@login_manager.user_loader def load_user(user_id): try: return models.User.get(models.User.id == user_id) except models.DoesNotExist: return None class User(UserMixin, Model): username = CharField(max_length=100) email = CharField(unique=True) passwd = CharField(max_length=100) joined_at = DateTimeField(default=datetime.datetime.now) user_confirmed = BooleanField(default=False) is_admin = BooleanField(default=False) class Meta: database = DATABASE order_by = ('-joined_at',)
I ran this code locally and it worked. Soon as i try to create a primary key like you did, i get the error ValueError: invalid literal for int() with base 10: 'None'
Bit confused, are you using models.User.select() or as your code suggest models.User.get()?
# Shouldn't it be User.uid rather than User.id. Also not sure why you are trying to convert it to an integer though. # try the below @login_manager.user_loader def load_user(user_id): try: return models.User.get(models.User.uid == user_id) except models.DoesNotExist: return None
hope this helps