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

jinja2.exceptions.TemplateNotFound: when use include function in template #863

Closed
so77id opened this issue Jun 5, 2018 · 1 comment
Closed

Comments

@so77id
Copy link

so77id commented Jun 5, 2018

I am trying to use this package without using any specific framework, and I am trying to create a template for a markdown file. Everything works fine when using a file as a template, but when using the include function within the template to call another file, it is giving me the error: jinja2.exceptions.TemplateNotFound

Template Code

template.md

# This is Markdown file
{% if a == True %}
  {% include './a.md' %}
{% else %}
  {% include './b.md' %}
{% endif %}
"""

a.md

this is a.md

b.md

this is b.md

example.py

from jinja2 import Environment, BaseLoader

with open('./template.md', 'r') as reader:
    file = reader.read()

template = Environment(loader=BaseLoader()).from_string(file)
render = template.render({"a": True})	

Full Traceback

  File "example.py", line 7, in <module>
    render = template.render({"a": True})
  File ".virtualenvs/cv3/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File ".virtualenvs/cv3/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File ".virtualenvs/cv3/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File ".virtualenvs/cv3/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 4, in top-level template code
  File ".virtualenvs/cv3/lib/python3.6/site-packages/jinja2/loaders.py", line 91, in get_source
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: a.md

Environment

  • Python version: Python 3.6.0
  • Jinja version: 2.10
@ThiefMaster
Copy link
Member

You need to use a FileSystemLoader (or any other loader that actually knows how to load a template given a name) if you want to reference other templates.

Also, please use IRC or Stack Overflow for this kind of question.

@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

2 participants