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 trialSavious Munyaradzi Ngundu
16,920 PointsNow use current_user.is_authenticated() to make the logout() link only show to authenticated users and the login() and r
please help me out where am i getting it wrong?
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.LoginForm()
if form.validate_on_submit():
try:
user = models.User.select().where(
models.User.email**form.email.data
).get()
if check_password_hash(user.password, form.password.data):
login_user(user)
flash("You're now logged in!")
return redirect(url_for('index'))
else:
flash("Email or password is invalid")
except models.DoesNotExist:
flash("Email or password is invalid")
return render_template('login.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')
<!doctype html>
<html>
<head>
<title>Lunch</title>
</head>
<body>
<nav>
{% 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>
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsI’ve tried your HTML template code and it passes for me. You should not need to alter the lunch.py
file. Post back or you are still having issues with this challenge. Good luck!
Brandon Harden
Python Web Development Techdegree Student 6,586 PointsThis answer should be updated to reflect that the parentheses in "{% if current_user.is_authenticated() %}" should be removed. According to the Alert in this video:
https://teamtreehouse.com/library/build-a-social-network-with-flask/broadcasting/add-some-layout
Chris Freeman
Treehouse Moderator 68,441 PointsThanks for the reference. As Kenneth said some things change, but alas sometimes they stay the same. The version of code run by this challenge checker seems to be stuck on the older version.
I’ve rerun the code with and without the parens. In this challenge it appears the parens are still required.