2017-02-03 15 views
-2

*******************フラスコを使用して掲示板を作成しようとしていますが、突然動作を停止しました。 help.Iフラスコで多くの経験を持っていないしてください、私はまた、変数グローバルフラスコUnboundLocalError:割り当て前にローカル変数 'shift1'が参照されています


を宣言しようとしたの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

+0

質問内のすべてのエラーをコードブロック –

答えて

1

時間の比較に欠陥があります。 0

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

秒コンポーネントのデフォルトなので、15:29:3015:29:00より後でです:あなたはtime()オブジェクトの秒コンポーネントを無視しています。これは、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] 
関連する問題