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 Broadcasting Stream Views

igsm '
igsm '
10,440 Points

Where does username come from (if username)?

I do not really understand where does username come from? We say if username =! ... but which username we are referring to?

@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() # ** compare if match
        stream = user.posts.limit(100)
    else: # our username
        stream = current_user.get_stream().limit(100)
        user = current_user
    if username:
        template = 'user_stream.html'
    return render_template(template, stream=stream, user=user)

1 Answer

Username is coming from the query string in the url "/stream/username"

So the idea being when you navigate to "/stream/username" you want to do a check to see if the request is for a anonymous user or the logged in user.

@app.route('/stream')
@app.route('/stream/<username>')
def stream(username=None):
    template = 'stream.html'
    # anonymous user
    if username and username != current_user.username:
        user = models.User.select().where(models.User.username**username).get() # ** compare if match
        stream = user.posts.limit(100)
    else: # our username
        stream = current_user.get_stream().limit(100)
        user = current_user
    if username:
        template = 'user_stream.html'
    return render_template(template, stream=stream, user=user)
igsm '
igsm '
10,440 Points

Alrightttt! Thank you Andreas.