[英]I am getting an error in python using flask and it is very hard to understand, is there something wrong with my code or the library?
I am using flask and python and I am making a website, I have two files, a python file and an html file .我正在使用 flask 和 python 并且我正在制作一个网站,我有两个文件,一个 python 文件和一个 ZFC35FZ8830D5FCC69D2386 文件I am also using Pycharm for this project.
我也在这个项目中使用 Pycharm。
Python file , "main.py" Python 文件,“main.py”
# Import and Setup
from flask import Flask, redirect, url_for, render_template
app = Flask(__name__)
# Functions
@app.route("/")
def home():
return render_template("template/index.html")
if __name__ == '__main__':
app.run()
HTML file , "index.html" HTML 文件,“index.html”
<!doctype html>
<html>
<head>
<title>Home page</title>
</head>
<body>
<h1>Home Page!</h1>
</body>
</html>
Error错误
* Serving Flask app "Main" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[2020-08-08 16:45:36,119] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "C:\Users\tohar\PycharmProjects\Website\venv\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\tohar\PycharmProjects\Website\venv\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\tohar\PycharmProjects\Website\venv\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\tohar\PycharmProjects\Website\venv\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\tohar\PycharmProjects\Website\venv\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\tohar\PycharmProjects\Website\venv\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:/Users/tohar/PycharmProjects/Website/Main.py", line 10, in home
return render_template("template/index.html")
File "C:\Users\tohar\PycharmProjects\Website\venv\lib\site-packages\flask\templating.py", line 138, in render_template
ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "C:\Users\tohar\PycharmProjects\Website\venv\lib\site-packages\jinja2\environment.py", line 930, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "C:\Users\tohar\PycharmProjects\Website\venv\lib\site-packages\jinja2\environment.py", line 883, in get_template
return self._load_template(name, self.make_globals(globals))
File "C:\Users\tohar\PycharmProjects\Website\venv\lib\site-packages\jinja2\environment.py", line 857, in _load_template
template = self.loader.load(self, name, globals)
File "C:\Users\tohar\PycharmProjects\Website\venv\lib\site-packages\jinja2\loaders.py", line 115, in load
source, filename, uptodate = self.get_source(environment, name)
File "C:\Users\tohar\PycharmProjects\Website\venv\lib\site-packages\flask\templating.py", line 60, in get_source
return self._get_source_fast(environment, template)
File "C:\Users\tohar\PycharmProjects\Website\venv\lib\site-packages\flask\templating.py", line 89, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: template/index.html
127.0.0.1 - - [08/Aug/2020 16:45:36] "GET / HTTP/1.1" 500 -
It is fine when I first run it, then when I open the site, the site says Error 500, internal server error and this error shows up in the terminal.我第一次运行它时很好,然后当我打开站点时,站点显示错误 500,内部服务器错误,并且此错误显示在终端中。 Im not sure if it is my code's problem or something else, how can i fix this?
我不确定这是我的代码问题还是其他问题,我该如何解决?
Sorry, I cannot embed images into my posts yet, Project Structure抱歉,我还不能将图片嵌入到我的帖子中,项目结构
Do you see the error?你看到错误了吗?
jinja2.exceptions.TemplateNotFound: template/index.html
You do not have a template
directory.您没有
template
目录。 So, try: return render_template("index.html")
所以,试试:
return render_template("index.html")
Better yet, make a templates
directory, and put the index.html
file inside.更好的是,创建一个
templates
目录,并将index.html
文件放入其中。 Flask will look inside the templates
directory by default. Flask 默认会在
templates
目录中查找。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.