简单 Python 和 Flask 问题 -- 404 错误
Simple Python and Flask question -- 404 error
我正在尝试制作一个程序来在网页上显示一些时间和温度数字。我有两个 html 模板,“index.html”和“set.html”在主“furnace”目录下的“templates”目录中。索引部分在被 flask 调用时正常工作,但“设置”link(转到页面以输入新的时间和日期)导致“404 未找到”错误。我已经从 Python 程序中删除了不属于 flask 的所有内容并显示 html,但我仍然收到错误。在这一点上,剩下的就和我找到的简单例子差不多了;我已经将我的程序与示例进行了比较,但我看不出出了什么问题。有眼尖的能看出我的错误吗?
熔炉-9x.py
import time
import datetime
import RPi.GPIO as GPIO
import smbus
from gpiozero import Button
from flask import Flask, render_template
import SDL_DS3231_dev
import lcddriver
import threading
# Using a global here for simplicity in this demo
# Globals are generally bad practice, so you would maintain
# the status of "lcd_counter" in a more elegant way
# (e.g. through a shared module, through classes, etc.)
# A simple global serves the purpose for this demo
# of a flask server running in parallel to another process
# emulating some I/O, printing to the console, etc.
lcd_counter = 0
app = Flask(__name__)
@app.route("/")
def index():
print('Index')
global datetimestr, temperature
global minutes_24,minutes_30,heatOn
return render_template('index.html')
@app.route("/set")
def setTime():
print("Set time")
return render_template('set.html')
if __name__ == '__main__':
#=== Prelude ===
app.run(debug=False, host='0.0.0.0')
index.html
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body {font-family:arial;
background-color: rgb(185,179,175);
text-align: left;}
table, tr {text-align: left;
font-size: 150%;
border-style: solid;}
</style>
</head>
<body>
<h1>Furnace</h1>
<table>
<tr>
<td colspan="3">{{dtstr}}</td>
</tr>
<tr>
<td>30 day: </td>
<td>{{m30}}</td>
<td>{{p30}}%</td>
</tr>
<tr>
<td>24 hour: </td>
<td>{{m24}}</td>
<td>{{p24}}%</td>
</tr>
<tr>
<td>Temp:</td>
<td> {{temper}}°F </td>
<td>Heat: {{heat}}</td>
</tr>
</table>
<p><a href="set.html">Set time</a></p>
</body>
</html>
set.html
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body {font-family:arial;
background-color: rgb(185,179,175);
text-align: left;}
table, tr {text-align: left;
font-size: 150%;
border-style: solid;}
</style>
</head>
<body>
<p>Enter the date and time:
<input type="text" name="datetime" size="15" maxlength="19" />
<input type="submit" value="Submit" />
</p>
</body>
</html>
这里的问题是 index.html (set.html) 中的 link 与您的路线 (/set) 不匹配。
要解决此问题,您可以像这样更新 index.html 中的第 35 行:
<p><a href="set">Set time</a></p>
flask 网络服务器完全匹配路由字符串,而不是模板的文件名。更好的是,您可以使用 flask 内置函数 url_for:
<p><a href="{{ url_for('setTime') }}">Set time</a></p>
我正在尝试制作一个程序来在网页上显示一些时间和温度数字。我有两个 html 模板,“index.html”和“set.html”在主“furnace”目录下的“templates”目录中。索引部分在被 flask 调用时正常工作,但“设置”link(转到页面以输入新的时间和日期)导致“404 未找到”错误。我已经从 Python 程序中删除了不属于 flask 的所有内容并显示 html,但我仍然收到错误。在这一点上,剩下的就和我找到的简单例子差不多了;我已经将我的程序与示例进行了比较,但我看不出出了什么问题。有眼尖的能看出我的错误吗?
熔炉-9x.py
import time
import datetime
import RPi.GPIO as GPIO
import smbus
from gpiozero import Button
from flask import Flask, render_template
import SDL_DS3231_dev
import lcddriver
import threading
# Using a global here for simplicity in this demo
# Globals are generally bad practice, so you would maintain
# the status of "lcd_counter" in a more elegant way
# (e.g. through a shared module, through classes, etc.)
# A simple global serves the purpose for this demo
# of a flask server running in parallel to another process
# emulating some I/O, printing to the console, etc.
lcd_counter = 0
app = Flask(__name__)
@app.route("/")
def index():
print('Index')
global datetimestr, temperature
global minutes_24,minutes_30,heatOn
return render_template('index.html')
@app.route("/set")
def setTime():
print("Set time")
return render_template('set.html')
if __name__ == '__main__':
#=== Prelude ===
app.run(debug=False, host='0.0.0.0')
index.html
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body {font-family:arial;
background-color: rgb(185,179,175);
text-align: left;}
table, tr {text-align: left;
font-size: 150%;
border-style: solid;}
</style>
</head>
<body>
<h1>Furnace</h1>
<table>
<tr>
<td colspan="3">{{dtstr}}</td>
</tr>
<tr>
<td>30 day: </td>
<td>{{m30}}</td>
<td>{{p30}}%</td>
</tr>
<tr>
<td>24 hour: </td>
<td>{{m24}}</td>
<td>{{p24}}%</td>
</tr>
<tr>
<td>Temp:</td>
<td> {{temper}}°F </td>
<td>Heat: {{heat}}</td>
</tr>
</table>
<p><a href="set.html">Set time</a></p>
</body>
</html>
set.html
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body {font-family:arial;
background-color: rgb(185,179,175);
text-align: left;}
table, tr {text-align: left;
font-size: 150%;
border-style: solid;}
</style>
</head>
<body>
<p>Enter the date and time:
<input type="text" name="datetime" size="15" maxlength="19" />
<input type="submit" value="Submit" />
</p>
</body>
</html>
这里的问题是 index.html (set.html) 中的 link 与您的路线 (/set) 不匹配。
要解决此问题,您可以像这样更新 index.html 中的第 35 行:
<p><a href="set">Set time</a></p>
flask 网络服务器完全匹配路由字符串,而不是模板的文件名。更好的是,您可以使用 flask 内置函数 url_for:
<p><a href="{{ url_for('setTime') }}">Set time</a></p>