我試圖通過制作一個記錄好的部分、壞部分(帶有廢代碼)、缺失部分和時間變數的燒瓶應用程式來簡化我的作業。
我在保存日志的頁面上有多個“按鈕”。我目前正在嘗試讓頁面重新加載當前按鈕狀態并在按下每個特定按鈕時存盤時間戳(在 python 腳本中)。
即我單擊開始按鈕,頁面重新加載所有復選標記和按鈕狀態,并在腳本中存盤一個 startTime 標記供以后使用,然后禁用開始按鈕。如果我單擊暫停按鈕,頁面將重新加載所有資料并禁用暫停按鈕并禁用停止按鈕,直到再次單擊暫停按鈕(在腳本中存盤時間戳)。如果我單擊停止按鈕,結果頁面將加載以格式化方式顯示所有總數。
我已經搜索、聯系、除錯、使用過開發人員工具。前一分鐘有效,下一分鐘崩潰。目前,主頁將加載,開始按鈕作業和禁用。當我單擊停止時,它崩潰了。
Traceback (most recent call last)
File "\dev\furnace-log\__init__.py", line 115, in home
if request.method == 'POST' and request.form['startTime'] == 'Start':
File "\dev\furnace-log\venv\Lib\site-packages\werkzeug\datastructures.py", line 377, in __getitem__
raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'startTime'
這是按鈕和表單的代碼。請注意,表單中洗掉了 jinja 的一些“if”陳述句。
<form method="post">
{% if startBtnClicked == 1 %}
<input class="btn btn-success" href="{{url_for('home')}}" type="submit" name="startTime" value="Start" disabled>
{% else %}
<input class="btn btn-success" href="{{url_for('home')}}" type="submit" name="startTime" value="Start">
{% endif %}
<input class="btn btn-warning" href="{{url_for('home')}}" type="submit" name="pauseTime" value="Pause">
<input class="btn btn-danger" href="{{url_for('home')}}" type="submit" name="stopTime" value="Stop">
</form>
這是回家路線的代碼。我讓它列印以控制每個 if 分支的任何資料以供參考。
@app.route('/home', methods=['GET', 'POST'])
def home():
form = LogNavButtons(request.form)
# When Start is clicked the time is stored to DB after checking if Start has already been clicked
if request.method == 'POST' and request.form['startTime'] == 'Start':
# check if any bool is true or false
if LogNavButtons.startActive == 0:
# get the time the furncase started logging and set startActive to true(1)
tm = time.time()
LogNavButtons.startActive = 1
flash('Now logging time and labels...', 'success')
print(tm, LogNavButtons.startActive,
LogNavButtons.stopActive, LogNavButtons.pauseActive)
# disable start button (show/inactive)
# set Stop and Pause to false (show/active)
return redirect(url_for('home'))
# check to see if the Pause button was pressed
elif request.method == 'POST' and request.form['pauseTime'] == 'Pause':
if LogNavButtons.pauseActive == 0:
tm = time.time()
print(tm, LogNavButtons.startActive,
LogNavButtons.stopActive, LogNavButtons.pauseActive)
LogNavButtons.pauseActive = 1
return redirect(url_for('home'))
# check to see if the Stop button has been pressed
elif request.method == 'POST' and request.form['endTime'] == 'Stop':
if LogNavButtons.stopActive == 0:
tm = time.time()
print(tm, LogNavButtons.startActive,
LogNavButtons.stopActive, LogNavButtons.pauseActive)
LogNavButtons.stopActive = 1
flash('All logging and counts have stopped...', 'danger')
print(tm, LogNavButtons.startActive,
LogNavButtons.stopActive, LogNavButtons.pauseActive)
return redirect(url_for('home'))
# if not, all else
else:
return render_template('home.html', title='Furnace Log', form=form)
我沒有足夠的信譽來鏈接帶有降價的影像,但這是正確加載后指向主路線影像的鏈接。 https://drive.google.com/file/d/1eCC_q_xJjGMObYVI_tJq6UJOePHCYgtj/view?usp=sharing
感謝您的時間、耐心和指導。
uj5u.com熱心網友回復:
堆疊跟蹤指明了方向。問題是為什么請求中沒有“startTime”。答案是按鈕不是表單域。如果您單擊“stopTime”按鈕,“startTime”將不會作為帖子的一部分發送。
一種前進的方式是注意這request.form是一種dict, 和改變
if request.method == 'POST' and request.form['startTime'] == 'Start':
到
if request.method == 'POST' and request.form.get('startTime', '') == 'Start':
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/443956.html
下一篇:通過Flask發送n行大檔案?
