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 trialGinny Pennekamp
31,466 PointsWhen I run app.py, why am I getting: builtins.AttributeError AttributeError: 'bool' object has no attribute '__call__'
The following lines are the ones coming up in the debugging code outside of /Library/Frameworks/Python.framework errors:
- Flask Social App/app.py", line 54, in index
return render_template('stream.html', stream=stream)
This refers to this function in my app.py:
@app.route('/')
def index():
"""Home page view, shows a stream of all Social App posts"""
stream = models.Post.select().limit(100)
return render_template('stream.html', stream=stream)
- Flask Social App/templates/stream.html", line 1, in top-level template code
{% extends "layout.html" %}
Here is my 'stream.html':
{% extends "layout.html" %}
{% block content %}
{% for post in stream %}
<article>
<h2>
<a href="{{ url_for('stream', username=post.user.username) }}">{{ post.user.username }}</a>
</h2>
<i class="clock"></i><time data-time="{{ post.timestamp }}" class="distime" datetime="{{ post.timestamp.strftime('%Y-%m-%d %H:%M:%S') }}">{{ post.timestamp.strftime('%Y-%m-%d %H:%M:%S') }}</time><a href="{{ url_for('view_post', post_id=post.id) }}" class="view">View</a>
<div class="post">
{{ post.content }}
</div>
</article>
{% endfor %}
{% endblock %}
- Flask Social App/templates/layout.html", line 27, in top-level template code
<h1>Hello{% if current_user.is_authenticated() %} {{current_user.username}}
{% endif %}!</h1>
My code for this part of 'layout.html' is:
<div class="grid-33">
<!-- Say Hi -->
<h1>Hello{% if current_user.is_authenticated() %} {{current_user.username}}{% endif %}!</h1>
</div>
Why am I getting these errors when I try to run the app on my computer? I have checked all my code against the downloaded S4V5 files, and I'm still getting this error.
7 Answers
Kenneth Love
Treehouse Guest TeacherPosting this here so people can see it, this comes from a change in how UserMixin
works in flask-login
. I'd tell you to install a specific version of flask-login
but...they removed those releases from PyPI.
So...instead of {{ current_user.is_authenticated() }}
, you need to use {{ current_user.is_authenticated }}
. Just don't include the parentheses. Yeah, it's an annoying change but, honestly, I think it's probably better code design.
Kenneth Love
Treehouse Guest TeacherCan you copy and paste the entire error message? I don't see anything in particular in the code you've pasted but maybe there's something we're missing.
Kamilla Holanda Crozara
3,377 PointsYeah! It worked for me. Thanks!
Ginny Pennekamp
31,466 PointsYes! Thank you for the help. Here's the whole error page: builtins.AttributeError AttributeError: 'bool' object has no attribute 'call'
Traceback (most recent call last) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1836, in call return self.wsgi_app(environ, start_response) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1820, in wsgi_app response = self.make_response(self.handle_exception(e)) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1403, in handle_exception reraise(exc_type, exc_value, tb) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/compat.py", line 33, in reraise raise value File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1817, in wsgi_app response = self.full_dispatch_request() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1477, in full_dispatch_request rv = self.handle_user_exception(e) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1381, in handle_user_exception reraise(exc_type, exc_value, tb) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise raise value File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1475, in full_dispatch_request rv = self.dispatch_request() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/app.py", line 1461, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/Users/Ginny/Desktop/Websites/Flask Social App/app.py", line 54, in index return render_template('stream.html', stream=stream) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/templating.py", line 128, in render_template context, ctx.app) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/flask/templating.py", line 110, in _render rv = template.render(context) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/environment.py", line 989, in render return self.environment.handle_exception(exc_info, True) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/environment.py", line 754, in handle_exception reraise(exc_type, exc_value, tb) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/jinja2/_compat.py", line 37, in reraise raise value.with_traceback(tb) File "/Users/Ginny/Desktop/Websites/Flask Social App/templates/stream.html", line 1, in top-level template code {% extends "layout.html" %} File "/Users/Ginny/Desktop/Websites/Flask Social App/templates/layout.html", line 27, in top-level template code <h1>Hello{% if current_user.is_authenticated() %} {{current_user.username}}{% endif %}!</h1> AttributeError: 'bool' object has no attribute 'call_' The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error. To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.
You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:
dump() shows all variables in the frame dump(obj) dumps all that's known about the object
Ginny Pennekamp
31,466 PointsWhen I sent this a couple of days ago, I was accessing the index view just fine, and the error message was only coming up when I tried to access any of the other views. Today, I got it right at the index, so there are different pieces of code referenced:
Here is my login view in app.py: @app.route('/login', methods=('GET', 'POST')) def login(): """Allows a user to log in""" form = forms.LoginForm() if form.validate_on_submit(): try: user = models.User.get(models.User.email == form.email.data) except models.DoesNotExist: flash("Your email or password doesn't match!", "error") else: if check_password_hash(user.password, form.password.data): login_user(user) flash("You've been logged in!", "success") return redirect(url_for('index')) else: flash("Your email or password doesn't match!", "error") return render_template('login.html', form=form)
My "login.html" looks like this: {% extends "layout.html" %} {% from 'macros.html' import render_field %}
{% block content %} <form method="POST" action="" class="form"> {{ form.hidden_tag() }} {% for field in form %} {{ render_field(field) }} {% endfor %} <button type="submit" id="submit">Login!</button> </form> {% endblock %}
Line 27 in "layout.html" is still: <h1>Hello{% if current_user.is_authenticated() %} {{current_user.username}}{% endif %}!</h1>
Ginny Pennekamp
31,466 Points@app.route('/login', methods=('GET', 'POST'))
def login():
"""Allows a user to log in"""
form = forms.LoginForm()
if form.validate_on_submit():
try:
user = models.User.get(models.User.email == form.email.data)
except models.DoesNotExist:
flash("Your email or password doesn't match!", "error")
else:
if check_password_hash(user.password, form.password.data):
login_user(user)
flash("You've been logged in!", "success")
return redirect(url_for('index'))
else:
flash("Your email or password doesn't match!", "error")
return render_template('login.html', form=form)
The above login view formatted correctly
Kenneth Love
Treehouse Guest TeacherWow, I don't see anything at all. OK, one more try. Can you zip up your entire project and email it to me?
Ginny Pennekamp
31,466 PointsOn its way... thanks for the help!
Timo Blume
10,107 PointsOh maaaaan thank you!! I had the same problem and tried everything without any success! Almost lost faith here... Thanks to Ginny for posting the question
Christopher Shaw
Python Web Development Techdegree Graduate 58,248 PointsChristopher Shaw
Python Web Development Techdegree Graduate 58,248 PointsThanks, had this error when I tried to copy my completed worspace project to my own virtual environment. This sorted it out.
jamalbacchus
Courses Plus Student 6,664 Pointsjamalbacchus
Courses Plus Student 6,664 PointsI had to downgrade flask-login from 0.4.0 to 0.2.11 to get everything to work.