flask的render_template载入html文件报错AttributeError

问题描述

我在尝试使运行在虚拟环境中的flask将html文件载入本地端口,我对照了示例代码(这个例子来自CS50)但没有找到出错的地方;我也根据网络搜索到的信息重新命名了函数名,但仍然报错,我非常希望能有人帮助我给出一些其他的思路。

这是我的报错信息
// 请把代码文本粘贴到下方(请勿用图片代替代码)

Traceback (most recent call last):
  File "c:\users\thinkpad\myproject\venv\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "c:\users\thinkpad\myproject\venv\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "c:\users\thinkpad\myproject\venv\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "c:\users\thinkpad\myproject\venv\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "c:\users\thinkpad\myproject\venv\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "c:\users\thinkpad\myproject\venv\lib\site-packages\flask\app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\Thinkpad\myproject\hello.py", line 6, in index
    return render_template("myindex.html")
AttributeError: 'function' object has no attribute 'html'
127.0.0.1 - - [22/Oct/2018 20:44:55] "GET / HTTP/1.1" 500 -

这是我的hello.py

from flask import Flask, render_template
app = Flask(__name__)

@app.route("/")
def index():
    return render_template("myindex.html")

这是我的myindex.html,放在templates文件夹下

<!DOCTYPE html>
<html>
    <head>
        <title>My Website</title>
    </head>
    <body>
        <h1>Hello world!</h1>
    </body>
</html>

我是在虚拟环境下hello.py所在的路径set FLASK_APP=hello.py, flask run运行的。

阅读 4.4k
1 个回答
  • 第一个问题,你的 hello.pytemplates 文件夹是在同一层目录里吗?
  • 第二个问题,你是用虚拟环境来执行 hello.py 文件的吗?
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题