flask UnboundLocalError:局部变量' shift1'在分配之前引用

时间:2017-02-03 18:49:52

标签: python flask

*********************我正在尝试使用烧瓶制作告示板,代码工作正常,但突然停止工作。请帮助。我没有多少经验,我也试图宣布变量全局

Python(烧瓶)代码:

from flask import Flask, render_template
import datetime
import MySQLdb

app = Flask(__name__)

@app.route('/shutdown', methods=['POST'])
def shutdown():
    shutdown_server()
    return 'Server shutting down...'




@app.route('/')  
def index():

  now = datetime.datetime.now()
  timestamp = datetime.datetime.now().time()

  if datetime.time(7, 30) <= timestamp <= datetime.time(15, 29):
    shift1 = "A"
  elif datetime.time(15, 30) <= timestamp <= datetime.time(23, 29):
    shift1 = "B"
  elif datetime.time(23, 30) <= timestamp <= datetime.time(7, 29):
    shift1 = "C"
  return render_template('hello.html', month=now.month, date=now.day, year=now.year, hour=now.hour, minute=now.minute, second=now.second, shift=shift1)

if __name__ == '__main__':
    app.run(host= '0.0.0.0', debug=True)

Html代码:

<html>
<head>
<script>
setInterval(function() {
                  window.location.reload();
                }, 1000); 

</script>

<style>
table, td, tr {
    text-align: center;
    height: 72px;
    font-size: 53;
}


</style>
</head>

<body>

<table border="1" width="100%" height="941px">
<tr>
<td colspan="4"><p align="centre"><font color="blue">{{ date }} / {{ month }} / {{ year }}</font></p></td>
<td colspan="4"><p align="centre"><font color="blue">{{ hour }} : {{ minute }} : {{ second }}</font></p></td>
</tr>
<tr>
<td colspan="5"></td>
<td colspan="2" ><font>Shift:</td>
<td colspan="1" ><font color="red">{{ shift }}</td>
</tr>

<tr>
<td colspan="2"  width="20%"><font size="28" color="green">Plan</td>
<td width="10%">1</td>
<td></td>
<td rowspan="8" colspan="4"></td>
</tr>
<tr>
<td colspan="2" rowspan="3"></td>
<td width="10%">2</td>
<td></td>
</tr>
<tr>
<td width="10%">3</td>
<td></td>

</tr>

<tr>
<td width="10%">4</td>
<td></td>

</tr>
<tr>
<td colspan="2"height="50px" width="20%"><font size="28" color="green">Actual</td>
<td width="10%">5</td>
<td></td>

</tr>
<td colspan="2" rowspan="3"></td>
<td width="10%">6</td>
<td></td>
</tr>
<tr>
<td width="10%">7</td>
<td></td>

</tr>

<tr>
<td width="10%">8</td>
<td></td>

</tr>
</table>
</body>
</html>

错误:

click here to see the screenshot

1 个答案:

答案 0 :(得分:1)

你的时间比较是有缺陷的;你忽略了time()对象的秒组件:

>>> import datetime
>>> datetime.time(15, 29, 30) <= datetime.time(15, 29)
False
>>> datetime.time(15, 29, 30) >= datetime.time(15, 30)
False

秒组件默认为0,因此15:29:30 以后而不是15:29:00。这意味着有些时间戳不会导致任何if .. elif分支匹配,并且永远不会设置shift1

改为使用<测试,并使用更好的上限:

if datetime.time(7, 30) < timestamp < datetime.time(15, 30):
    shift1 = "A"
elif datetime.time(15, 30) < timestamp < datetime.time(23, 30):
    shift1 = "B"
elif datetime.time(23, 30) < timestamp < datetime.time(7, 30):
    shift1 = "C"

对于更多的班次,我会使用二分;设置序列中的开始时间,然后使用当前时间的结果插入点作为一系列班次的索引:

from bisect import bisect

shift_starts = (datetime.time(7, 30), datetime.time(15, 30), datetime.time(23, 30))
shift_names = 'CABC'  # C twice to account for the time before 7:30
shift_index = bisect(shift_starts, timestamp)
shift1 = shift_names[shift_index]