简体   繁体   English

我在使用 flask 的 python 中遇到错误,很难理解,我的代码或库有问题吗?

[英]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.

相关问题 我的 python 代码中出现此错误。 “OSError:[WinError 10038] 尝试对不是套接字的东西进行操作” - I am getting this error in my python code. "OSError: [WinError 10038] An operation was attempted on something that is not a socket" 我收到烧瓶错误 404 但我的代码是正确的 - I am getting flask error 404 but my code is correct 需要帮助来理解 python 代码 - 我是 Python 的初学者 - Need assistance to understand python code - I am very beginner in Python 我正在尝试使用 Python 和 Flask 将新用户插入到我的 sqlAlchemy 中,但一直出现错误 - I am trying to insert new user to my sqlAlchemy by using Python and Flask but keep getting an error 在flickr上将CSS选择器与beautifulsoup结合使用时遇到困难,我做错了吗? - Having a hard time using CSS selector with beautifulsoup on flickr, am i doing something wrong? 我在 VS Code 中使用随机库时遇到错误 - I am facing an error using random library in my VS Code 为什么我的 python 代码中出现 Traceback 错误 - Why am I getting Traceback error in my python code 为什么我的 python 代码出现值错误 - Why am I getting a Value error on my python code 我使用flask-oauthlib库在POST / api / submit上一直找不到错误 - I am keep getting not found error on the POST /api/submit using flask-oauthlib library 我不明白我的 python 代码有什么问题 - I cant understand what's wrong with my python code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM