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

Kyle Barney
Kyle Barney
9,081 Points

Flask App: AttributeError: 'unicode' object has no attribute 'view_user_events'

Background: Flask app

I'm trying to pull a parameter out of a url and pass it to the app to make the appropriate call from the model, but I'm getting this error whenever I try to visit the url:

AttributeError: 'unicode' object has no attribute 'view_user_events'

Here's the code in which the error is occurring:

@app.route('/timeline/<username>')
def get_user_timeline(username):
    event_list = username.view_user_events(username)
    def other_timeline(username=None):
        if username and username != current_user.username:
            try:
                user = models.User.select().where(
                    models.User.username**username).get()
            except models.DoesNotExist:
                abort(404)
            else:
                event_list = user.events
        else:
            return redirect(url_for('timeline'))
        if username:
            return render_template('userpages/timeline.html', event_list=event_list, user=user) 

How can I get rid of this error? (And feel free to point out any other flagrant mistakes - I'm a newbie working through this, so there are likely others.)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Hey Kyle, looking at your code, the error is in line 3:

@app.route('/timeline/<username>')
def get_user_timeline(username):  # <-- username pass in as unicode string argument
    event_list = username.view_user_events(username)   # <-- trying to run username.view_user_events() method
    # Error the unicode object 'username' does not have a method called 'view_user_events()'
    # You likely want to get the 'event_list' after you figure out the 'user' below

    def other_timeline(username=None):
        if username and username != current_user.username:
            try:
                user = models.User.select().where(
                    models.User.username**username).get()
            except models.DoesNotExist:
                abort(404)
            else:
                event_list = user.events
        else:
            return redirect(url_for('timeline'))
        # HERE, now that you have a 'user' object or returned with a 404,
        # you can get the list of user events with using 'view_user_events' 
        # event_list = .....view_user_events()   # <-- implementation dependent

        if username:
            return render_template('userpages/timeline.html', event_list=event_list, user=user)