Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template imported/rendered twice #391

Closed
jp-costa opened this issue Dec 16, 2014 · 5 comments
Closed

Template imported/rendered twice #391

jp-costa opened this issue Dec 16, 2014 · 5 comments

Comments

@jp-costa
Copy link

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>
@ThiefMaster
Copy link
Member

Looking at how ['mytemplate.html'] is printed twice, too, it looks a lot like your script contains two print statements of each type... I've tried it on both Linux and Windows and can't reproduce it.

If you can reproduce it, please create a full testcase showing the issue (and put it e.g. in a Git repo).

@ho-ho-ho
Copy link

ho-ho-ho commented 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 ;)

@Dotrox
Copy link

Dotrox commented Aug 20, 2016

I have the same issue on Ubuntu 14.04 with Python 2.7.11, Jinja2 2.8 and Flask 0.11.1.
And I am sure that my code runs only once!

Note: this happens only when I open some page first time after restart of the server! When I update page - templates are loaded once.

@davidism
Copy link
Member

davidism commented 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.

@davidism davidism closed this as completed Oct 7, 2019
@chapsticks

This comment has been minimized.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants