我正在使用Python Flask來託管主要工作的Web服務器。我理解這個問題,但我不知道如何解決這個問題。我知道在顯示的代碼中,當網站首次啓動時會渲染模板,變量「CurtainCloseTime」沒有賦值。所以我在我的網站上有兩種形式,我輸入窗簾來關閉和打開變量名稱。變量「CCT」意味着CurtainCloseTime,它是HTML文件中的變量,因爲我希望它顯示回網頁。Python中的Flask&HTML Local變量(UnboundLocalError)
這裏是Python瓶代碼:
@app.route('/', methods=['GET','POST'])
def index():
if request.method == 'POST': #If there is an input from form
CurtainOpenTime = request.form['CurtainOpenTime'] #Assign CurtainOpenTime variable in python to the html CurtainOpenTime variable.
CurtainCloseTime = request.form['CurtainCloseTime'] #Assign CurtainCloseTime variable in python to the html CurtainCloseTime variable.
return render_template('Index.html', CCT=CurtainCloseTime)
那麼當'request.method'是** not **設置爲''POST''時會發生什麼?在這種情況下應該呈現什麼? –
我修復了縮進,這是因爲我不得不重寫代碼而不是複製和粘貼。當我設置request.method =='GET'所以不POST,我得到400錯誤的請求錯誤。 – ThePGT
我並沒有建議你改變測試,我要求你考慮GET應該怎麼做。當您收到「POST」方法請求時,變量'CurtainCloseTime'僅爲* set *。你得到這個錯誤是因爲你沒有把它設置在'GET'的情況下。 –