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 Post Form and View

Arnaldo Acosta
Arnaldo Acosta
15,870 Points

return redirect(url_for('index')) return render_template('post.html', form=form)

I'm actually a bit confused as to why we are doing: return redirect(url_for('index')) return render_template('post.html', form=form)

I mean back in Flask Basics I assumed we were using the url_for('index') because we actually had an index.html file but that's no the case here. This kinda got me all confused and now I don't even know what these two do either together or separately. I'd appreciate if someone could help me out understanding these concepts.

Thanks very much in advance!

1 Answer

Amber Diehl
Amber Diehl
15,402 Points

Hi Arnaldo,

I don't know if you're still looking for an answer to this but thought I would respond.

Regarding the first "return": return redirect(url_for('index'))

For each route we define, we associate a view, e.g.

@app.route('/', methods=['GET', 'POST']) def index(): ...

The route '/' is now intrinsically linked to index().

The url_for() is looking up the path for the view called 'index' (in this case the path is '/') and redirecting the user to that path.

Regarding the second "return", we are rendering the template "post.html" template with the form as context to the user.

Hope this helps!