我正在开发Flask应用,该应用将从我的sqlite3数据库中获取数据并将其推送到index.html。由于某种原因,我在执行代码时遇到错误:
from flask import Flask, render_template, request
app = Flask(__name__)
import sqlite3
# Retrieve data from database
def getData():
conn=sqlite3.connect('../Sensors.db')
curs=conn.cursor()
for row in curs.execute("SELECT * FROM BME_DATA ORDER BY TIME_STAMP DESC LIMIT 1"):
Time = str(row[1])
Temperature = row[2]
Gas = row[3]
Humidity = row[4]
Pressure = row[5]
Altitude = row[6]
conn.close()
return Time, Temperature, Gas, Humidity, Pressure, Altitude
# main route
@app.route("/")
def index():
Time, Temperature, Gas, Humidity, Pressure, Altitude = getData()
templateData = {
'Time': Time,
'Temperature': Temperature,
'Gas': Gas,
'Humidity': Humidity,
'Pressure': Pressure,
'Altitude': Altitude
}
return render_template('index.html', **templateData)
if __name__ == "__main__":
app.run(debug=False)
烧瓶应用程序位于BMEWebServer文件夹中。我的结构如下所示:
BMEWebServer/appBMEWebServer.py
BMEWebServer/templates/style.css
BMEWebServer/static/index.html
我得到的错误是:
>>> %Run appBMEWebServer.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[2019-03-01 01:45:32,774] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python3/dist-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python3/dist-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/lib/python3/dist-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python3/dist-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/pi/Desktop/Sensors_Database/BMEWebServer/appBMEWebServer.py", line 30, in index
return render_template('static/index.html', **templateData)
File "/usr/lib/python3/dist-packages/flask/templating.py", line 133, in render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 851, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 812, in get_template
return self._load_template(name, self.make_globals(globals))
File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 774, in _load_template
cache_key = self.loader.get_source(self, name)[1]
File "/usr/lib/python3/dist-packages/flask/templating.py", line 57, in get_source
return self._get_source_fast(environment, template)
File "/usr/lib/python3/dist-packages/flask/templating.py", line 85, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: static/index.html
127.0.0.1 - - [01/Mar/2019 01:45:32] "GET / HTTP/1.1" 500 -
127.0.0.1 - - [01/Mar/2019 01:45:34] "GET /favicon.ico HTTP/1.1" 404 -
有什么想法我在这里做错了吗?
答案 0 :(得分:1)
好吧,实际上您的css
文件和html
需要交换。正确的结构应为:
BMEWebServer/appBMEWebServer.py
BMEWebServer/templates/index.html
BMEWebServer/static/style.css
答案 1 :(得分:1)
该错误来自文件的结构方式。只需将index.html文件获取到BueWebServer / templates / index.html的Tue右侧文件夹中,并对CSS进行同样的操作,即BMEWebServer / static / styles.css。 Jinja非常聪明,可以渲染正确的路径。
答案 2 :(得分:-1)
非常感谢Sraw和Douglas,您是对的。当我确实更改了正确目录中的文件时,它确实起作用了,但实际上,我的文件夹模板的末尾没有加“ s”的名字,实际上还有一个问题。重命名后,我就可以浏览页面了。 非常感谢