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 A Social Network with Flask Broadcasting Stream Views

Chuwen Tan
Chuwen Tan
5,786 Points

Why do we need to type `template = 'user_stream.html'` if there has been `template = 'stream.html'` ?

In the flask course, when we want to create a stream.html to show either our or others' own social network page, why do we need to type template = 'user_stream.html' if there has been template = 'stream.html' ? I thought that when we type template='stream.html', we have already tell the program to go to stream.html. Can someone help explain the following code from the course the teacher wrote?

And what is the template='user_stream.html' stuff? This is the first time it showed in the course but lack of explanation. Thank you!

@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.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)