[PENDING] Broken Lesson - Build Web Apps with Flask - Logout Exercise

And yet another COMPLETELY broken lesson! The instructions don’t even make sense. The thing they are asking for is already in imports! Write the html code? Where? I am completely frustrated at these lessons because I haven’t learned a darn thing! You guys really need to fix this section!!

4 Likes

I just want to second this request.

It’s really disappointing to get this deep into the Build Python Web Apps with Flask path only to find most of this Accounts and Authentication module has broken lessons with minimal, sometimes nonsensical instructions.

Please fix this!

Totally agree, very disappointing :frowning:

It is still broken. Disappointing. I agree with this thread

+1, today still broken lesson

still broken 11/28/2020. Where can I go to get my 30 mins back!

Logout

Hi,
My code works on the Codecademy page. I always try to run the code on localhost.
On localhost the logout method is’nt works.
I can’t find the issue. Can anyone more experienced coder take a look to my code, please?

app,py
import flask
from flask_sqlalchemy import SQLAlchemy
# Import logout_user below:
from flask_login import LoginManager, UserMixin, login_required, login_user, logout_user
from flask import request, render_template, flash, redirect,url_for
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash


app = flask.Flask(__name__)
app.secret_key = 'secretkeyhardcoded'
login_manager = LoginManager()
login_manager.init_app(app)
db = SQLAlchemy(app)

class User(UserMixin,db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    email = db.Column(db.String(120), index=True, unique=True)
    password = db.Column(db.String(128))

    def __repr__(self):
        return '<User {}>'.format(self.username)

@login_manager.user_loader
def load_user(id):
    return User.query.get(int(id))
    
@app.route('/', methods=['GET', 'POST'])
def index():
  if flask.request.method == 'GET':
    return '''
    <p>Your credentials:
    username: TheCodeLearner
    password: !aehashf0qr324*&#W)*E!
    </p>
               <form action='/' method='POST'>
                <input type='text' name='email' id='email' placeholder='email'/>
                <input type='password' name='password' id='password' placeholder='password'/>
                <input type='submit' name='submit'/>
               </form>
               '''
  email = "TheCodeLearner"
  if flask.request.form['password'] == "!aehashf0qr324*&#W)*E!":
    user = User(email="TheCodeLearner@gmail.com", username="TheCodeLearner",password="!aehashf0qr324*&#W)*E!")
    login_user(user)
    return render_template("logged_in.html", current_user=user )
  return login_manager.unauthorized()

@app.route('/home')
@login_required
def home():
	return render_template('logged_in.html')

@login_manager.unauthorized_handler
def unauthorized():
    # do stuff
    return "You are not logged in. Click here to get <a href="+ str("/")+">back to Landing Page</a>"

@app.route("/logout")
@login_required
def logout():
    logout_user()
    return redirect(url_for('index'))

When I click on the logout link I get the following error on the command line:

Error message:
[2021-02-04 16:46:29,061] ERROR in app: Exception on /logout [GET]
Traceback (most recent call last):
  File "c:\users\admin\appdata\local\programs\python\python38-32\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "c:\users\admin\appdata\local\programs\python\python38-32\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "c:\users\admin\appdata\local\programs\python\python38-32\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "c:\users\admin\appdata\local\programs\python\python38-32\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "c:\users\admin\appdata\local\programs\python\python38-32\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "c:\users\admin\appdata\local\programs\python\python38-32\lib\site-packages\flask\app.py", line 1936, in dispatch_request    
    return self.view_functions[rule.endpoint](**req.view_args)
  File "c:\users\admin\appdata\local\programs\python\python38-32\lib\site-packages\flask_login\utils.py", line 270, in decorated_view
    elif not current_user.is_authenticated:
  File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\werkzeug\local.py", line 347, in __getattr__
    return getattr(self._get_current_object(), name)
  File "C:\Users\admin\AppData\Roaming\Python\Python38\site-packages\werkzeug\local.py", line 306, in _get_current_object
    return self.__local()
  File "c:\users\admin\appdata\local\programs\python\python38-32\lib\site-packages\flask_login\utils.py", line 26, in <lambda>      
    current_user = LocalProxy(lambda: _get_user())
  File "c:\users\admin\appdata\local\programs\python\python38-32\lib\site-packages\flask_login\utils.py", line 346, in _get_user    
    current_app.login_manager._load_user()
  File "c:\users\admin\appdata\local\programs\python\python38-32\lib\site-packages\flask_login\login_manager.py", line 318, in _load_user
    user = self._user_callback(user_id)
  File "C:\Utils\Python\Codecademy\Learn Flask\Accounts and Authentication\Introduction to Authentication with Flask\app.py", line 26, in load_user
    return User.query.get(int(id))
ValueError: invalid literal for int() with base 10: 'None'
127.0.0.1 - - [04/Feb/2021 16:46:29] "GET /logout HTTP/1.1" 500 -

I can reproduce the same error message by running:

print(int("None"))

So the problem is:
The /logout router call the load_user(id) function with "None" parameter and raise an error on localhost but run well on the codecademy environment.

  • Codecademy use: Python 3.6.9 (default, Jul 17 2020, 12:50:27) [GCC 8.4.0]
  • On localhost run: Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:37:30) [MSC v.1927 32 bit (Intel)]