Flask app jinja 2 错误在 flask 网站请帮我解决这个问题
Flask app jinja 2 error in flask website Please help me resolve this issue
文件结构在底部:
[文件结构][1]
错误是:
jinja2.exceptions.TemplateNotFound
jinja2.exceptions.TemplateNotFound: entry.html
完整错误是:
[jinja 2 完整错误][2]
除 /entry 文件外,所有文件均正常工作(检查下面的 flask_app 代码)
**我正在使用 python 版本 3 和烧瓶来制作 website.I 还导入了 jinja 2 **
**警告:不要将所有代码复制并粘贴到一个文件中 FILE.There 是不同的文件。
所有文件代码都给你了
import search4letters
app = Flask(__name__)
@app.route('/search4')
def do_search() -> 'html':
return str(search4letters('life, the universe, and everything!', 'eiru,!'))
@app.route('/')
@app.route('/entry')
def entry_page() -> 'html':
return render_template('entry.html', the_title = "Welcome to search4letters on the web!")
app.run(debug=True)
**The entry.html file code is here: **
{% extends 'base.html' %}
{% block body %}
<h2>{{ the_title }}</h2>
<form method='POST' action='/search4'>
<table>
<p>Use this form to submit a search request:</p>
<tr><td>Phrase:</td><td><input name='phrase' type='TEXT'
width='60'></td></tr>
<tr><td>Letters:</td><td><input name='letters' type='TEXT'
value='aeiou'></td></tr>
</table>
<p>When you're ready, click this button:</p>
<p><input value='Do it!' type='SUBMIT'></p>
</form>
{% endblock %}
###The base.html file code is here: ###
<!doctype html>
<html>
<head>
<title>{{ the_title }}</title>
<link rel="stylesheet" href="static/hf.css" />
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>
**I have made the static folder. Inside it is the hf.css file. hf.css file code is given here: **
body {
font-family: Verdana, Geneva, Arial, sans-serif;
font-size: medium;
background-color: grey;
/* background-color: tan; */
margin-top: 5%;
margin-bottom: 5%;
margin-left: 10%;
margin-right: 10%;
border: 1px dotted gray;
padding: 10px 10px 10px 10px;
}
a {
text-decoration: none;
font-weight: 600;
}
a:hover {
text-decoration: underline;
}
a img {
border: 0;
}
h2 {
font-size: 150%;
}
table {
margin-left: 20px;
margin-right: 20px;
caption-side: bottom;
border-collapse: collapse;
}
td, th {
padding: 5px;
text-align: left;
}
.copyright {
font-size: 75%;
font-style: italic;
}
.slogan {
font-size: 75%;
font-style: italic;
}
.confirmentry {
font-weight: 600;
}
/*** Tables ***/
table {
font-size: 1em;
background-color: #fafcff;
border: 1px solid #909090;
color: #2a2a2a;
padding: 5px 5px 2px;
border-collapse: collapse;
}
td, th {
border: thin dotted gray;
}
/*** Inputs ***/
input[type=text] {
font-size: 115%;
width: 30em;
}
input[type=submit] {
font-size: 125%;
}
select {
font-size: 125%;
}```
**Please help me resolve this problem.**
###results.html file code is given here - This file gives the results of to the user.###
{% extends 'base.html' %}
{% block body %}
<h2>{{ the_title }}</h2>
<p>You submitted the following data:</p>
<table>
<tr><td>Phrase:</td><td>{{ the_phrase }}</td></tr>
<tr><td>Letters:</td><td>{{ the_letters }}</td></tr>
</table>
<p>When "{{the_phrase }}" is search for "{{ the_letters }}", the following
results are returned:</p>
<h3>{{ the_results }}</h3>
{% endblock %}
[1]: https://i.stack.imgur.com/B38GN.png
[2]: https://i.stack.imgur.com/RIo1H.png
您需要确保所有 HTML 文件都放在应用程序根目录下的 templates
文件夹中。 Flask 结构包含两个标准文件夹,static
和 templates
:
- 静态文件夹包含模板使用的资源,包括
CSS 个文件、JavaScript 个文件和图像。
- 模板文件夹仅包含模板。它们有一个 .html 扩展名。
render_template
的默认目录为 /templates
,因此如果此处不存在模板,您将收到 TemplateNotFound
异常。
Flask 应用程序的一般结构布局是:
my-flask-app
├── static/
│ └── css/
│ └── main.css
├── templates/
│ ├── base.html
│ └── entry.html
├── flask_app.py
文件结构在底部:
[文件结构][1]
错误是:
jinja2.exceptions.TemplateNotFound
jinja2.exceptions.TemplateNotFound: entry.html
完整错误是:
[jinja 2 完整错误][2]
除 /entry 文件外,所有文件均正常工作(检查下面的 flask_app 代码)
**我正在使用 python 版本 3 和烧瓶来制作 website.I 还导入了 jinja 2 **
**警告:不要将所有代码复制并粘贴到一个文件中 FILE.There 是不同的文件。
所有文件代码都给你了
import search4letters
app = Flask(__name__)
@app.route('/search4')
def do_search() -> 'html':
return str(search4letters('life, the universe, and everything!', 'eiru,!'))
@app.route('/')
@app.route('/entry')
def entry_page() -> 'html':
return render_template('entry.html', the_title = "Welcome to search4letters on the web!")
app.run(debug=True)
**The entry.html file code is here: **
{% extends 'base.html' %}
{% block body %}
<h2>{{ the_title }}</h2>
<form method='POST' action='/search4'>
<table>
<p>Use this form to submit a search request:</p>
<tr><td>Phrase:</td><td><input name='phrase' type='TEXT'
width='60'></td></tr>
<tr><td>Letters:</td><td><input name='letters' type='TEXT'
value='aeiou'></td></tr>
</table>
<p>When you're ready, click this button:</p>
<p><input value='Do it!' type='SUBMIT'></p>
</form>
{% endblock %}
###The base.html file code is here: ###
<!doctype html>
<html>
<head>
<title>{{ the_title }}</title>
<link rel="stylesheet" href="static/hf.css" />
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>
**I have made the static folder. Inside it is the hf.css file. hf.css file code is given here: **
body {
font-family: Verdana, Geneva, Arial, sans-serif;
font-size: medium;
background-color: grey;
/* background-color: tan; */
margin-top: 5%;
margin-bottom: 5%;
margin-left: 10%;
margin-right: 10%;
border: 1px dotted gray;
padding: 10px 10px 10px 10px;
}
a {
text-decoration: none;
font-weight: 600;
}
a:hover {
text-decoration: underline;
}
a img {
border: 0;
}
h2 {
font-size: 150%;
}
table {
margin-left: 20px;
margin-right: 20px;
caption-side: bottom;
border-collapse: collapse;
}
td, th {
padding: 5px;
text-align: left;
}
.copyright {
font-size: 75%;
font-style: italic;
}
.slogan {
font-size: 75%;
font-style: italic;
}
.confirmentry {
font-weight: 600;
}
/*** Tables ***/
table {
font-size: 1em;
background-color: #fafcff;
border: 1px solid #909090;
color: #2a2a2a;
padding: 5px 5px 2px;
border-collapse: collapse;
}
td, th {
border: thin dotted gray;
}
/*** Inputs ***/
input[type=text] {
font-size: 115%;
width: 30em;
}
input[type=submit] {
font-size: 125%;
}
select {
font-size: 125%;
}```
**Please help me resolve this problem.**
###results.html file code is given here - This file gives the results of to the user.###
{% extends 'base.html' %}
{% block body %}
<h2>{{ the_title }}</h2>
<p>You submitted the following data:</p>
<table>
<tr><td>Phrase:</td><td>{{ the_phrase }}</td></tr>
<tr><td>Letters:</td><td>{{ the_letters }}</td></tr>
</table>
<p>When "{{the_phrase }}" is search for "{{ the_letters }}", the following
results are returned:</p>
<h3>{{ the_results }}</h3>
{% endblock %}
[1]: https://i.stack.imgur.com/B38GN.png
[2]: https://i.stack.imgur.com/RIo1H.png
您需要确保所有 HTML 文件都放在应用程序根目录下的 templates
文件夹中。 Flask 结构包含两个标准文件夹,static
和 templates
:
- 静态文件夹包含模板使用的资源,包括 CSS 个文件、JavaScript 个文件和图像。
- 模板文件夹仅包含模板。它们有一个 .html 扩展名。
render_template
的默认目录为 /templates
,因此如果此处不存在模板,您将收到 TemplateNotFound
异常。
Flask 应用程序的一般结构布局是:
my-flask-app
├── static/
│ └── css/
│ └── main.css
├── templates/
│ ├── base.html
│ └── entry.html
├── flask_app.py