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 trialJoseph Michelini
Python Development Techdegree Graduate 18,692 PointsHow 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
Treehouse TeacherHi 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!