question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for Jinja2 HTML templates (for a Flask template)

See original GitHub issue

I am in the process of making a Flask cookiecutter template, but I’m running into problems when the Flask Jinja templates are added.

As an example, say I have this template:

<p>Go <a href="{{ url_for('home') }}">Home</a></p>

Then when I run cookiecutter cookiecutter-flask, I get a jinja2.exceptions.UndefinedError:

Traceback (most recent call last):
  File "/Users/sloria1/Envs/cookiecutter-flask/bin/cookiecutter", line 8, in <module>
    load_entry_point('cookiecutter==0.6.1', 'console_scripts', 'cookiecutter')()
  File "/Users/sloria1/Envs/cookiecutter-flask/lib/python2.7/site-packages/cookiecutter/main.py", line 90, in main
    cookiecutter(args.input_dir)
  File "/Users/sloria1/Envs/cookiecutter-flask/lib/python2.7/site-packages/cookiecutter/main.py", line 61, in cookiecutter
    context=context
  File "/Users/sloria1/Envs/cookiecutter-flask/lib/python2.7/site-packages/cookiecutter/generate.py", line 88, in generate_files
    rendered_file = tmpl.render(**context)
  File "/Users/sloria1/Envs/cookiecutter-flask/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/Users/sloria1/Envs/cookiecutter-flask/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "./cookiecutter-pypackage/{{cookiecutter.repo_name}}/{{cookiecutter.repo_name}}/templates/home.html", line 1, in top-level template code
    <p>Go <a href="{{ url_for('home') }}">Home</a></p>
jinja2.exceptions.UndefinedError: 'url_for' is undefined

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
pydannycommented, Aug 14, 2013

I had a similar problem with creating a Django cookiecutter template (https://github.com/pydanny/cookiecutter-dj-package) that I need to go fix. 😉

I think escaping (http://jinja.pocoo.org/docs/templates/#escaping) is the answer. Try:

{{ {{ url_for('home') }} }}

If that doesn’t work, try:

{% raw %}
<p>Go <a href="{{ url_for('home') }}">Home</a></p>
{% endraw %}
2reactions
audreyfeldroycommented, Aug 14, 2013

You can also escape just the {{ like this:

{{ "{{" }}

Same for escaping }} with {{ "}}" }}.

I do something similar to escape { here: https://github.com/audreyr/cookiecutter-jquery/blob/master/{{cookiecutter.repo_name}}/src/jquery.{{cookiecutter.repo_name}}.coffee#L40

The downside is that Jinja-templated templates are not the prettiest to read. (Any ideas how to improve this, anyone?)

I will add this to the documentation when I get a chance, since it’s a common scenario.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rendering Pages in Flask Using Jinja - Hackers and Slackers
We'll start by creating an app that uses 1 of each of the three template types: layout.html, home.html, and nav.html. These templates will...
Read more >
HTML Templates with Jinja - Flask Tutorial - techwithtim.net
This flask tutorial will show you how to use the HTML template engine called jinja. It allows you to write dynamic HTML code...
Read more >
Templates — Flask Documentation (2.2.x)
This section only gives a very quick introduction into how Jinja2 is integrated into Flask. If you want information on the template engine's...
Read more >
Learn Flask: Jinja2 Templates and Forms Cheatsheet
Templates are enabled using the Jinja2 template engine and allow data to be ... When it comes to HTML web forms, the Flask...
Read more >
Primer on Jinja Templating - Real Python
With Jinja, you can build rich templates that power the front end of ... Create a new template named base.html in your templates/...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found