[英]Using the same template name multiple times with Flask and Jinja2
我有一個具有以下結構的Flask項目:
├── config.py
├── errors
│ ├── __init__.py
│ ├── handlers.py
│ └── templates
│ ├── errors
│ │ ├── 403.html
│ │ ├── 404.html
│ │ └── 500.html
│ └── default_layout.html
├── home
│ ├── __init__.py
│ ├── routes.py
│ └── templates
│ ├── about
│ │ └── general.html
│ └── default_layout.html
├── static
│ └── style.css
└── templates
└── default_layout.html
我的應用程序包含兩個藍圖:錯誤和主頁。
有沒有辦法讓我的藍圖中的模板文件擴展具有相同文件名的多個模板文件? 我有以下導致異常的內容:
1)app / errors / templates / errors / 403.html:
{% extends 'default_layout.html' %}
{% block error_content %}
<div class="content-section">
<h1>404 error</h1>
<p>{{ error }}</p>
</div>
{% endblock error_content %}
2)app / errors / templates / default_layout.html:
{% extends 'default_layout.html' %}
{% block content %}
<div class="content-section">
<h1>NESTED BLOCK</h1>
{% block error_content %}{% endblock %}
</div>
{% endblock content %}
3)app / templates / default_layer.html:
{% block content %}{% endblock %}
我想default_layout.html
在1中引用)來指代2)和default_layout.html
2參考)來指代3)。
一種方法是使用藍圖的名稱作為范圍進一步嵌套模板。 例如,
errors/templates/default_layout.html
會成為
errors/templates/errors/default_layout.html
並且使用它會變成{% extends "errors/default_layout.html" %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.