S
S
Sergey Ilyin2022-01-29 14:18:58
Flask
Sergey Ilyin, 2022-01-29 14:18:58

Why doesn't inheritance work in jinja?

Project structure:
- file with python code at the root
-- template / template.html
-- includes / years.html

file template.html

<!doctype html>
<html lang="ru">

<head>
    <title>123</title>
</head>

<body>

    {% block year %}
    {% endblock %}

</body>
</html>


years.html file
{% extends '../template/template.html' %}
{% block year %}
    {% for i in years %}
        {{ i }}
    {% endfor %}
{% endblock %}


python:
from jinja2 import Environment, DictLoader, Template, FileSystemLoader
file_loader = FileSystemLoader('includes')
env = Environment(loader=file_loader)
template = env.get_template('years.html')
years_list = [1992, 1993, 1994]
out_render = template.render(years = years_list)
print(out_render)


python crashes on adding a dictionary over the years to the template and says that it cannot find template.html:
TemplateNotFound: ../template/template.html
Tell me, what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Popov, 2022-02-16
@ademaro

Add a second folder to use as templates:

from jinja2 import Environment, DictLoader, Template, FileSystemLoader
file_loader = FileSystemLoader(['templates', 'includes'])
env = Environment(loader=file_loader)
template = env.get_template('years.html')
years_list = [1992, 1993, 1994]
out_render = template.render(years = years_list)
print(out_render)

and in the template just inherit{% extends 'template.html' %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question