Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!