Answer the question
In order to leave comments, you need to log in
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>
{% extends '../template/template.html' %}
{% block year %}
{% for i in years %}
{{ i }}
{% endfor %}
{% endblock %}
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)
TemplateNotFound: ../template/template.html
Answer the question
In order to leave comments, you need to log in
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)
{% extends 'template.html' %}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question