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 Flask Basics Character Builder Forms

Joseph Michelini
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Joseph Michelini
Python Development Techdegree Graduate 18,692 Points

How were we able to use url_for in index.html before we imported it?

Right before we import redirect and url_for, we use url_for in index.html, test the code and it successfully finds the /save route.

<form action="{{ url_for('save') }}" method="POST">

I was just curious why this works before we import url_for.

Thanks!

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Joseph Michelini ! The short answer is: we don't need to import anything into a template for it to work. I know it looks that way, but a Flask template has access to a few variables just out of the box. Because index.html is a Flask template, it has access to url_for just naturally. The same is not true for app.py. I recommend taking a look at the Flask template documentation for information on what variables/methods are available to template a just "out of the box".

Hope this helps! :sparkles: