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 How to Win Friends Follow and Unfollow Buttons

AttributeError: 'AnonymousUserMixin' object has no attribute 'username'

Getting the above error when trying to click on my stream on the website after doing the Follow and Unfollow video, it appears to point to;

File "/home/treehouse/workspace/app.py", line 105, in stream if username and username != current_user.username:

not sure what is wrong here as I did not edit this, and it was working before doing this video.

@app.route('/stream')
@app.route('/stream/<username>')
def stream(username=None):
    template = 'stream.html'
    if username and username != current_user.username:
        user = models.User.select().where(models.User.username**username).get()
        stream = user.post.limit(100)
    else:
        stream = current_user.get_stream().limit(100)
        user = current_user
    if username:
        template = 'user_stream.html'
    return render_template(template, stream=stream, user=user)

I have imported current_user from flask.ext.login

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

The issue is that current_user is not logged in and therefore does not have a defined username.

One solution is to check if the current_user Is logged in by wrapping your code with:

if current_user.is_authenticated():
    #code

A more advanced solution is to customize the anonymous user class from this stackoverflow post:

from flask.ext.login import AnonymousUserMixin
class Anonymous(AnonymousUserMixin):
  def __init__(self):
    self.username = 'Guest'

Then added this class to anonymous_user

login_manager.anonymous_user = Anonymous

First solution fixed it right up, thank you Chris.

Christopher Shaw
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Shaw
Python Web Development Techdegree Graduate 58,248 Points

Using Chris Freeman first suggestion, I did the following to check if the user is authenticated and gave a dummy name if not, which did not require any new imports.

@app.route('/stream')
@app.route('/stream/<username>')
def stream(username=None):
    template = 'stream.html'
    if current_user.is_authenticated():
        cur_usr_name = current_user.username
    else:
        cur_usr_name = "***no_actual_user_name_should_ever_match_this***"
    if username and username != cur_usr_name:
        try:
            user = models.User.select().where(
                models.User.username**username).get()
        except models.DoesNotExist:
            abort(404)
        else:
            stream = user.posts.limit(100)
    else:
        stream = current_user.get_stream().limit(100)
        user = current_user
    if username:
        template = 'user_stream.html'
    return render_template(template, stream=stream, user=user)
Alex Holigores
Alex Holigores
2,815 Points

Utilizing the AnonymousUserMixin solution I also added the following:

elif current_user.username == 'Guest':
        flash('Please login in to see your stream!', 'error')
        return redirect(url_for('index'))