Python A Social Network with Flask Broadcasting Add current_user Check

tariqasghar
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
tariqasghar
UX Design Techdegree Graduate 30,506 Points

Unexplained error again.

Again, no identification of the place where there's error. Just a general message: "Try again".

Help appreciated.

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, current_user, login_required, logout_user

import forms
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('/secret')
@login_required
def secret():
    return "I should only be visible to logged-in users"

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


@app.route('/')
def index():
    return render_template('index.html')
templates/layout.html
<!doctype html>
<html>
<head>
<title>Lunch</title>
</head>
<body>

<nav>
    <!-- Add menu here -->
    {% if current_user.is_authenticated() %}
    <a href="{{ url_For('logout') }}" title="Sign Out"></a>
    {% else %}
    <a href="{{ url_For('login') }}" title="Sign In"></a>
    <a href="{{ url_For('register') }}" title="Sign Up"></a>
    {% endif %}
</nav>

<div class="messages">
{% with messages = get_flashed_messages() %}
{% for message in messages %}
<div>{{ message }}</div>
{% endfor %}
{% endwith %}
</div>

{% block content %}{% endblock %}
</body>
</html>

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 25,376 Points

Looks like you understand the task and most of the syntax-- good work. Be careful with case, url_for is all in lower case. Also, the <a> tag must have its text in between the opening and closing tag. <a href="#">Your Text</a>

Good luck with your Python journey!

Spoilers below...

Part 1 just asks for links

<nav>
  <a href="{{ url_for('logout') }}">Sign Out</a>
  <a href="{{ url_for('login') }}">Sign In</a>
  <a href="{{ url_for('register') }}">Sign Up</a>
</nav>

Then, part 2

<nav>
  {% if current_user.is_authenticated() %}
  <a href="{{ url_for('logout') }}">Sign Out</a>
  {% else %}
  <a href="{{ url_for('login') }}">Sign In</a>
  <a href="{{ url_for('register') }}">Sign Up</a>
  {% endif %}
</nav>