![](/img/trans.png)
[英]I'm facing a TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement
[英]Flask TypeError: The view function for '**' did not return a valid response. The function either returned None or ended without a return statement
我正在學習 Flask,我使用這個框架開始了我的第一個項目。 目前我想將用戶輸入保存到數據庫中,但我得到了一個
TypeError:“bookingPage”的視圖 function 未返回有效響應。
function 要么返回 None 要么在沒有返回語句的情況下結束。 我檢查了我的 function,如果是 GET 請求,return 語句必須返回預訂頁面,如果是 POST 請求,則必須重定向到主頁。
我還在這里查看了有關此類錯誤的其他帖子,但有諸如“您需要添加返回語句”之類的答案。 在我的情況下,return 語句存在
這是我的 function 的代碼:
@app.route('/booking', methods=['POST', 'GET'])
def bookingPage():
if request.method == 'POST':
firstname = request.form['firstname']
lastname = request.form['lastname']
phone = request.form['phone']
email = request.form['email']
birthdate = request.form['birthdate']
booking_date = request.form['booking_date']
booking_time = request.form['booking_time']
guests_nr = request.form['guests_nr']
notes = request.form['notes']
reservation = Booking(CustomerFname=firstname, CustomerLname=lastname, CustomerPhone=phone, CustomerEmail=email,
CustomerBirthdate=birthdate, ReservationDate=booking_date, ReservationTime=booking_time,
NumberOfGuests=guests_nr, CustomerNotes=notes)
try:
db.session.add(reservation)
db.session.commit()
return redirect('/')
except:
return "An error has been occurred. Please, try again later"
else:
return render_template('booking.html')
這里也是 html 的代碼
<div class="booking-container">
<form method="post">
<input type="text" name="firstname" id="firstname" class="form-control" placeholder="First Name" required>
<input type="text" name="lastname" id="lastname" class="form-control" placeholder="Last Name" required>
<input type="text" name="email" id="email" class="form-control" placeholder="Email" required>
<input type="text" name="phone" id="phone" class="form-control" placeholder="Phone Number" required>
<input type="text" name="birthdate" id="birthdate" class="form-control" placeholder="Date of Birth" required>
<input type="text" name="booking_date" id="booking_date" class="form-control" placeholder="Reservation Date" required>
<input type="text" name="booking_time" id="booking_time" class="form-control" placeholder="Reservation Time" required>
<input type="text" name="guests_nr" id="guests_nr" class="form-control" placeholder="Number of Guests" required>
<textarea name="notes" id="notes" class="form-control" placeholder="Notes"></textarea>
<input type="submit" class="btn-submit" value="Sent">
</form>
</div>
問題解決了。 我在那里犯了一個愚蠢的錯誤:
在 def booking(): if request.method == 'POST':
必須是 ['POST'] 而不是 'POST'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.