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 Staff

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: