嗨,我有一個HTML文件中的窗體。使用request.form使用Python和Flask處理輸入數據。不過,我希望一些輸入字段是可選的。目前我收到以下錯誤:我可以使HTML輸入表單域可選嗎?
400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
我相信這是由於某些字段爲空。
我的形式被設計成當一個單選按鈕被選中,有些字段只出現一種方法。當表單被提交併且單選按鈕沒有被選中時,問題就會出現,因爲表單會將它們視爲空。
這裏是我的表單代碼:
<form action = "makeBooking" method = "POST" >
Booking ID <input type = "number" name = "bookingid" ><br>
Customer ID <input type = "number" name = "customerid" ><br>
<input type="radio" name="choice">
<label for="choice">Tick if new customer</label>
<div class="reveal-if-active">
First Name <input type = "test" name = "firstname" VALUE="x" ><br>
Surname <input type = "text" name = "surname" VALUE="x"><br>
Billing Address <input type = "text" name = "address" VALUE="x" ><br>
Email <input type = "text" name = "email" VALUE="x"><br>
</div>
Flight ID <input type = "number" name = "flightid" ><br>
Number of Seats <input type = "number" name = "numofseats" ><br>
<input type = "submit" value = "Submit" >
</form>
以下Python代碼就是如果單選按鈕沒有被選中,因爲他們提交的空的錯誤所在。
bookingid = request.form['bookingid']
customerid = request.form['customerid']
flightid = request.form['flightid']
numseats = request.form['numofseats']
if request.form['choice']:
firstname = request.form['firstname']
surname = request.form['surname']
address = request.form['address']
email = request.form['email']
#do some SQL insert statements with ALL the form data
else:
#do some sql insert statements with the none hidden statements
任何人有任何想法。
謝謝。
更新:爲了更清楚地顯示我的代碼。
你應該只執行在單選按鈕的選擇所提到的Python代碼,所以給出的答案可能會做的伎倆不處理這一點。無關;你不應該使用複選框而不是單獨的單選按鈕嗎?您無法輕易取消選中這些內容,因此用戶會錯誤地強制他們填寫表單的顯示部分。 –