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! While you're at it, check out some resources Treehouse students have shared here.

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

Python Build a Social Network with Flask Takin' Names Controlling sessions

Hannah Lee
Hannah Lee
80,187 Points

Now add a view named logout() with "/logout" as its route. This view should log the user out and then redirect to

I am stuck in this part...please help

lunch.py
from flask import (Flask, g, render_template, flash, redirect, url_for)
from flask.ext.bcrypt import check_password_hash
from flask.ext.login import (LoginManager, login_user, logout_user,
                             current_user, login_required)
import models

app = Flask(__name__)
app.secret_key = 'this is our super secret key. do not share it with anyone!'
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'


@login_manager.user_loader
def load_user(userid):
    try:
        return models.User.select().where(
            models.User.id == int(userid)
        ).get()
    except models.DoesNotExist:
        return None


@app.before_request
def before_request():
    g.db = models.DATABASE
    g.db.connect()
    g.user = current_user


@app.after_request
def after_request(response):
    g.db.close()
    return response


@app.route('/register', methods=('GET', 'POST'))
def register():
    form = forms.SignUpInForm()
    if form.validate_on_submit():
        models.User.new(
            email=form.email.data,
            password=form.password.data
        )
        flash("Thanks for registering!") 
    return render_template('register.html', form=form)


@app.route('/login', methods=('GET', 'POST'))
def login():
    form = forms.SignUpInForm()
    if form.validate_on_submit():
        try:
            user = models.User.get(
                models.User.email == form.email.data
            )
            if check_password_hash(user.password, form.password.data):
                login_user(user)
                flash("You're now logged in!")
            else:
                flash("No user with that email/password combo")
        except models.DoesNotExist:
              flash("No user with that email/password combo")
    return render_template('register.html', form=form)

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

@app.route('/secret')
@login_required
def secret():
    return "I should only be visible to logged-in users"

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Hannah,

You have two issues.

If you click the preview button you will see an error message:

Traceback (most recent call last): File "8ea5b41f-1274-416a-af8b-324f4d853332.py", line 144, in """) File "", line 29, in File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/werkzeug/test.py", line 830, in get return self.open(*args, **kw) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/testing.py", line 108, in open follow_redirects=follow_redirects) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/werkzeug/test.py", line 803, in open response = self.run_wsgi_app(environ, buffered=buffered) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/werkzeug/test.py", line 716, in run_wsgi_app rv = run_wsgi_app(self.application, environ, buffered=buffered) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/werkzeug/test.py", line 923, in run_wsgi_app app_rv = app(environ, start_response) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/app.py", line 1836, in __call__ return self.wsgi_app(environ, start_response) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/app.py", line 1820, in wsgi_app response = self.make_response(self.handle_exception(e)) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/app.py", line 1403, in handle_exception reraise(exc_type, exc_value, tb) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise raise value File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/app.py", line 1817, in wsgi_app response = self.full_dispatch_request() File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/app.py", line 1477, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/app.py", line 1381, in handle_user_exception reraise(exc_type, exc_value, tb) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise raise value File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/app.py", line 1475, in full_dispatch_request rv = self.dispatch_request() File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/app.py", line 1461, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "", line 51, in login NameError: name 'forms' is not defined

The important part is the name 'forms' is not defined'. Sure enough, if you look at your code, you'll see that you refer toformsin various places but you don't have aformsimport. If you compare your code to the supplied code for the challenge, you'll note that the supplied code does have thisforms` import.

Once you have reinstated this required import, you'll get a new error message:

Traceback (most recent call last): File "7f15e081-4000-4f80-9be5-e73f2f3ad857.py", line 144, in """) File "", line 46, in File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/werkzeug/test.py", line 830, in get return self.open(*args, **kw) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/testing.py", line 108, in open follow_redirects=follow_redirects) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/werkzeug/test.py", line 803, in open response = self.run_wsgi_app(environ, buffered=buffered) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/werkzeug/test.py", line 716, in run_wsgi_app rv = run_wsgi_app(self.application, environ, buffered=buffered) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/werkzeug/test.py", line 923, in run_wsgi_app app_rv = app(environ, start_response) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/app.py", line 1836, in __call__ return self.wsgi_app(environ, start_response) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/app.py", line 1820, in wsgi_app response = self.make_response(self.handle_exception(e)) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/app.py", line 1403, in handle_exception reraise(exc_type, exc_value, tb) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise raise value File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/app.py", line 1817, in wsgi_app response = self.full_dispatch_request() File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/app.py", line 1477, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/app.py", line 1381, in handle_user_exception reraise(exc_type, exc_value, tb) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise raise value File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/app.py", line 1475, in full_dispatch_request rv = self.dispatch_request() File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/app.py", line 1461, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask_login.py", line 758, in decorated_view return func(*args, **kwargs) File "", line 71, in logout File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/helpers.py", line 312, in url_for return appctx.app.handle_url_build_error(error, endpoint, values) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/app.py", line 1641, in handle_url_build_error reraise(exc_type, exc_value, tb) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise raise value File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/flask/helpers.py", line 305, in url_for force_external=external) File "/usr/local/pyenv/versions/3.6.4/lib/python3.6/site-packages/werkzeug/routing.py", line 1776, in build raise BuildError(endpoint, values, method, self) werkzeug.routing.BuildError: Could not build url for endpoint '/login'. Did you mean 'login' instead?

Again, the important part is at the end of the traceback: Could not build url for endpoint '/login'. Did you mean 'login' instead?. Your code looks like this:

    return redirect(url_for('/login'))

And if you double-check the video that comes before this challenge, you'll see (at about 1:47) that when using url_for you omit the leading slash character.

Hope that clears everything up for you. Cheers

Alex