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.

Template imported/rendered twice

See original GitHub issue

I am just getting started with jinja2 and I ran into a really weird issue, also described on issue #255 (closed, but there was no followup by the reporter).

I’m on Windows 7 x64 running Python 3.4.1 I installed jinja2 with “easy_install Jinja2”

I have a …\jinja2-test\jinja2-test.py containing this:

from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('jinja2-test', 'templates'))

template = env.get_template('mytemplate.html')

print(env.list_templates())
print(template.render(the='variables', go='here'))

and a template …\jinja2-test\templates\mytemplate.html

<html>
<head></head>

<body>
    <h1>Rendered template</h1>
    This template shows that the {{ the }} go {{ go }}
</body>

</html>

on the command line I do: python jinja2-test.py and the templates are listed and rendered twice:

...\jinja2-test> python jinja2-test.py
['mytemplate.html']
<html>
<head></head>

<body>
    <h1>Rendered template</h1>
    This template shows that the variables go here
</body>

</html>
['mytemplate.html']
<html>
<head></head>

<body>
    <h1>Rendered template</h1>
    This template shows that the variables go here
</body>

</html>

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

16reactions
ho-ho-hocommented, Feb 6, 2015

Had the same issue on arch linux with python 3.4.2

The PackageLoader constructor is running the whole script a second time, it’s not 2 prints in the script itself. Use FileSystemLoader instead and it works fine:

from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('templates'))

The documentation was a little misleading there 😉

1reaction
davidismcommented, Oct 7, 2019

This is equivalent to creating a file example.py that imports itself:

data = "foo"

import example

print(data)

python example.py first imports example as __main__, then it imports itself as example. Because of the way the import cache works, this results in two executions of the module. That’s just Python, it’s not something Jinja can do anything about.

If you set up a real package with __init__.py and run it as a module, or as an entry point, this doesn’t happen. Or put the main code in an if __name__ == "__main__": block.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is ng-template in Angular 10 rendered twice?
I figured out a problem with twice rendered components when a ng-template and ... import { Component, OnInit, Input, ViewChild, ...
Read more >
Using a template twice on the same page - Google Groups
Is there a way to use a template twice on the same page? We don't want to have to create a duplicate template...
Read more >
Import Permit Program - Frequently Asked Questions - CDC
A detailed description of how the material was rendered noninfectious. Is there a fee for obtaining a CDC import permit? No. Currently, there...
Read more >
Phoenix LiveView v0.18.3 - HexDocs
LiveView provides rich, real-time user experiences with server-rendered HTML. ... For each LiveView in the root of a template, mount/3 is invoked twice: ......
Read more >
Stories for multiple components - Storybook - JS.ORG
It's useful to write stories that render two or more components at once if those components are designed to work together. For example,...
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found