Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja ignores HTML comments [duplicate]

Not a question per sae but an observation others may find helpful.

I have had issues with
'werkzeug.routing.BuildError: Could not build url for endpoint ... '
errors when using Flask to build a website.

As part of my development I had created a menu list item with a number of items with links as
<a href="{{ url_for('home') }}">Home</a> ...

Later I wanted to modify the menus so commented out the HTML lines containing the original definitions with a <!-- ... --> block. Struggling to get the code working, and to understand the syntax and relationships better, I modified single entities, the .py def name(), HTML file names, url_for() statements etc. to observe the impact.

Having understood the .py function name relating to the @app.route() decorator, as an aide memoir, I modified the home page function name in the .py to be "py_home_fn" and modified the url_for() to read url_for('py_home_fn') to match giving
<a href="{{ url_for('py_home_fn') }}">Home</a>

I was perplexed to get

'werkzeug.routing.BuildError: Could not build url for endpoint 'home'. Do you mean 'py_home_fn' instead?'

being reported . Looking at the line number in the console, I noted that the line containing the error was within the commented section.

Changing the url_for('home') to be url_for('py_home_fn') in the HTML commented section, the error was no longer returned.

As a python/Flask nubie, this took me longer than I'd hoped and I haven't, so far, found any online references to this behaviour, although there are probably loads out there, so wanted to share.

like image 405
mhwalmsley Avatar asked Feb 16 '18 11:02

mhwalmsley


1 Answers

The html comment tag <!-- ... --> does not work in a jinja template. For commenting use the jinja comment tag ie, {# ... #}

like image 135
George J Padayatti Avatar answered Oct 04 '22 18:10

George J Padayatti