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
Ryan Ruscett
23,309 Pointsmacros.html template not found?
Ok, I am building a social network with flask here and I am using workspaces for the first time.
When I use the macros.html inside my register.html. It throws a "templateNotFound macros.html error"
Now If I put strait up html inside the register.html it renders and works fine. So it's exclusive to this macros.html not being able to be found. Now I reference it as macros.html the files name IS macros.html AND they are both in the templates folder. Yet, it still can't find it? I have to be missing something here.
Any thoughts out there??
Here is the register.html
{% from 'macros.html' import render_field %}
<form method="POST" action= "" class="form">
{{ form.hidden_tag() }}
{% for field in form %}
{{ render_field(field) }}
{% endfor %}
</form>
Here is the macros.html
{% macro render_field(field) %}
<div class='field'>
{% if field.errors %}
{% for error in field.errors %}
<div class="notification error">{{ error }}</div>
{% endfor %}
{% endif %}
{{ field(placeholder=field.label.text) }}
</div>
{% endmacro %}
3 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Ryan,
I finally figured it out. Your macros.html file name has a leading space.
After forking your workspace and verifying the problem I kept staring at the code and not finding a problem.
I then created a new file named macros2.html and copied your code into that file. After updating the import statement it worked.
I decided to use the console to look at the file names. That was when I discovered the leading space.
Here's some console output showing a few of the commands you could use to discover the problem:
treehouse:~/workspace/templates$ ls -b
\ macros.html register.html
treehouse:~/workspace/templates$ ls -1
macros.html
register.html
treehouse:~/workspace/templates$ ls -Q
" macros.html" "register.html"
treehouse:~/workspace/templates$
the -b option will print escape sequences for non graphical characters.
I think we don't see this in the left column of workspaces because the browser is going to collapse the whitespace in the html. I used dev tools to find the a tags that are wrapping the filenames and added white-space: pre; to the styles and then I was able to see the leading space on the filename. It's indented slightly more than register.html
When you go to rename the file to fix it you'll be able to see at that point the leading space.
Ryan Ruscett
23,309 PointsI am knees deep in some supercross right now but I will totally try this. I think you are on to something here. Space in the file name sheesh. I would not of caught that. Ill try it and let you know!!! Thanks for responding though because I am stuck. Ill gry it tonight actually just give me a bit.
Ryan Ruscett
23,309 PointsThat was it, space in the name of the file. I don't think I would of caught that. Even when I was playing around with it, I renamed it to macro.html and still never noticed the space in the beginning of the file name. You the man! Thanks!
Chris Freeman
Treehouse Moderator 68,468 PointsThese seems like UI bug with Workspaces that leading spaces are even possible. Reporting to Dev team.
Ryan Ruscett
23,309 PointsChris Freeman I totally agree.
Jason Anello
Courses Plus Student 94,610 PointsHey Chris,
I'm not sure that it's a bug if the underlying file system allows it. As far as I know, Windows, linux, and os x all allow spaces to be in filenames.
If you update the import statement to include the leading space then it would have worked out.
{% from ' macros.html' import render_field %}
So it seems that the underlying file system has no problem in dealing with the leading space.
I was able to create a file with a leading space in sublime text. So I think if it's disallowed in workspaces then it's not going to have the same behavior as other software.
Plus you can create a file with leading spaces in the console so you'd have to decide where you're going to draw the line on not allowing leading spaces.
At the very least, I think the proper html or css could be put in place to preserve these leading spaces. I only tried the css in my answer but using pre tags in the html should achieve the same result. Maybe that's the bug, not preserving the spaces in the source html.
In sublime text I could tell the file had a leading space but you can't in workspaces because of how the browser handles the whitespace.
Of course, it's another matter whether it's a good idea to use filenames with spaces or not.
In any case, let us know if anything comes out of it.
Ryan Ruscett
23,309 PointsAnother valid point. Lol
Josh Keenan
20,315 PointsHey Ryan, you forgot to tell Flask where the content block is!
{% from 'macros.html' import render_field %}
{% block content %}
<form method="POST" action= "" class="form">
{{ form.hidden_tag() }}
{% for field in form %}
{{ render_field(field) }}
{% endfor %}
</form>
{% endblock %}
Try that out!
Ryan Ruscett
23,309 PointsHey, See, the error is it can't find macros.html. Now whether or not the block content was there, would only really refer to the content on the page to be rendered. Except, my error says it can't even find the template to render. So that solution wont work.
Regardless I tried your solution considering I have no other options except to do virtualenv activate it and load it into pycharm or eclipse pydev to actually get some debugging on this. Annddd it's still doesn't work. I am open if you have any other suggestions though. Thanks for trying because I appear to be stuck at the moment.
Ryan Ruscett
23,309 PointsThe real question is how come the macros.html is not found? The name of the file is correct and it's in the templates folder. I just don't understand this and in workspaces it's impossible to debug.
Chris Freeman
Treehouse Moderator 68,468 PointsHey Ryan,
I took the opposite approach, I took a working workspace and hobbled it backwards to get toward your code:
- Commented out `extends layout.html' code
- Commented out
block contenttags - I tried overwriting my code with your
registerandmacroHTML files.
It all still worked.
The only way I could cause the jinja template not found error was to misname the macros.html file to macro.html (dropping the 's'). Maybe it's 'htm' instead of 'html'. Please check the filename in the workspace.
Reference: working Workspace with your code: https://w.trhou.se/dfhgowdyjj
Ryan Ruscett
23,309 PointsHey, thanks for the response. Yeah like you the first thing I thought was, can't find macros.html. So naturally I thought htm instead of html and then I thought I spelled it wrong. I am 100 percent positive the names are right. UNLESS my eyes are literally deceiving me, which is the only real logical thing I can think of lol. let me snapshot my workspace and send it. Than you can see for yourself.
Ryan Ruscett
23,309 PointsSNAPSHOT here it is.
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsIt might be easier for someone to track down the error if you can post a snapshot of your workspace.