Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Ricardo Sierra
Ricardo Sierra
14,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?

Ricardo Sierra
Ricardo Sierra
14,920 Points

My code is as follows:

In app.py:

@login_manager.user_loader
def load_user(user_id):
    try:
        return models.User.get(models.User.id == user_id)
    except models.DoesNotExist:
        return None

In models.py:

class User(UserMixin, Model):
    uid = PrimaryKeyField()
    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',)

2 Answers

Hi Ricardo,

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'

Ricardo Sierra
Ricardo Sierra
14,920 Points

Thank you very much!! I changed it and it worked!

Hi Ricardo

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

Ricardo Sierra
Ricardo Sierra
14,920 Points

I am using models.User.get().
I have tried the code you suggested but i keep getting:

" ValueError: invalid literal for int() with base 10: 'None' "

Ricardo Sierra
Ricardo Sierra
14,920 Points

The whole error message is:

Traceback (most recent call last): File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1836, in call return self.wsgi_app(environ, start_response)

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1820, in wsgi_app response = self.make_response(self.handle_exception(e))

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1403, in handle_exception reraise(exc_type, exc_value, tb)

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask_compat.py", line 33, in reraise raise value

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1817, in wsgi_app response = self.full_dispatch_request()

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request rv = self.handle_user_exception(e)

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1381, in handle_user_exception reraise(exc_type, exc_value, tb)

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask_compat.py", line 33, in reraise raise value

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request rv = self.dispatch_request()

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1461, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args)

File "C:\Users\pc\Documents\S4V5\paperapp\routes.py", line 120, in login return render_template('login.html', form=form)

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\templating.py", line 126, in render_template ctx.app.update_template_context(context)

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 716, in update_template_context context.update(func())

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask_login.py", line 891, in _user_context_processor return dict(current_user=_get_user())

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask_login.py", line 858, in _get_user current_app.login_manager._load_user()

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask_login.py", line 383, in _load_user return self._load_from_cookie(request.cookies[cookie_name])

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask_login.py", line 431, in _load_from_cookie self.reload_user()

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask_login.py", line 351, in reload_user user = self.user_callback(user_id)

File "C:\Users\pc\Documents\S4V5\paperapp\routes.py", line 27, in load_user return models.User.get(models.User.id == userid)

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 4505, in get return sq.get()

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 2885, in get return next(clone.execute())

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 2932, in execute self._qr = ResultWrapper(model_class, self._execute(), query_meta)

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 2627, in _execute sql, params = self.sql()

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 2900, in sql return self.compiler().generate_select(self)

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 1796, in generate_select return self.build_query(clauses, alias_map)

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 1680, in build_query return self.parse_node(Clause(*clauses), alias_map)

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 1641, in parse_node sql, params, unknown = self._parse(node, alias_map, conv)

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 1615, in _parse sql, params = self._parse_map[node_type](node, alias_map, conv)

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 1543, in _parse_clause node.nodes, alias_map, conv, node.glue)

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 1658, in parse_node_list node_sql, node_params = self.parse_node(node, alias_map, conv)

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 1641, in parse_node sql, params, unknown = self._parse(node, alias_map, conv)

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 1615, in _parse sql, params = self._parse_map[node_type](node, alias_map, conv)

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 1524, in _parse_expression rhs, rparams = self.parse_node(node.rhs, alias_map, conv)

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 1643, in parse_node params = [conv.db_value(i) for i in params]

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 1643, in <listcomp> params = [conv.db_value(i) for i in params]

File "C:\Users\pc\AppData\Local\Programs\Python\Python35-32\lib\site-packages\peewee.py", line 928, in db_value return value if value is None else self.coerce(value)

ValueError: invalid literal for int() with base 10: 'None'