MrC*_*ujo 2 jinja2 flask python-3.x
我的烧瓶项目中有以下结构:
app
-app
-static
-templates
-layouts
footer.html
header.html
main.html
search.html
__init__.py
app.py
MANIFEST.in
setup.py
Run Code Online (Sandbox Code Playgroud)
在应用程序.py中:
@app.route('/search')
def show_search_form():
return render_template('search.html')
Run Code Online (Sandbox Code Playgroud)
搜索.html:
{% extends "layouts/main.html" %}
{% block body %}
Test
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
主要.html
{% include 'header.html' %}
{% block content %}
{% endblock %}
{% include 'footer.html' %}
Run Code Online (Sandbox Code Playgroud)
header.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>App</title>
</head>
<body>
Run Code Online (Sandbox Code Playgroud)
页脚.html
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我将 main.html 扩展为 search.html 并使用(在本例中)“Test”作为要注入到正文块中的内容。但它不起作用,我收到以下错误:
jinja2.exceptions.TemplateNotFound: header.html
Run Code Online (Sandbox Code Playgroud)
代码有什么问题吗?
由于您的 footer.html 和 header.html 文件位于布局目录下,因此您需要在主模板中引用它们:
主要.html
{% include 'layouts/header.html' %}
{% block content %}
{% endblock %}
{% include 'layouts/footer.html' %}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2812 次 |
| 最近记录: |