sqlite BadRequestKeyError:400错误请求:浏览器(或代理)发送了一个此服务器无法理解的请求,KeyError:'email'

zz2j4svz  于 2023-05-18  发布在  SQLite
关注(0)|答案(1)|浏览(196)

我正在创建一个小型巴士预订网站,列出目的地和时间。当我执行我的flask代码时,除了book.html页面外,每个页面都执行得很好,不知何故,它保持自我重定向到home.html页面并显示标题中列出的错误消息,为了更好地查看,这里是错误消息error message display screenshot的屏幕截图,这里是我的两个db浏览器的图片,用于sqlite数据库表database tables,(https://i.stack.imgur.com/d25hv.png
flask 代码:

from flask import Flask, render_template, request, redirect, url_for
import sqlite3 
import os

currentdirectory = os.path.dirname(os.path.abspath(__file__))

app=Flask(__name__)

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/home")
def home():
        return render_template("home.html")

@app.route("/home", methods = ["POST", "GET"])
def myhome():
     if request.method =="POST":
        email = request.form["email"]
        password = request.form["password"]
        connection = sqlite3.connect(currentdirectory + "\database.db")
        cursor = connection.cursor()
        query1 = "SELECT username, password from Users_Details WHERE username = '{un}' AND password = '{pw}'".format(un = email, pw = password)
     
        cursor.execute(query1)
        rows = cursor.fetchall()
        if len(rows) == 0:
          print("try again")
        else:
         return render_template("book.html")
      
     return render_template("home.html")
@app.route("/signup", methods = ["GET", "POST"])
def signup():
     if request.method == "POST":
          email = request.form["email"]
          password = request.form["password"]
          number = request.form["number"]
          address = request.form["address"]
          connection = sqlite3.connect(currentdirectory + "\database.db")
          cursor = connection.cursor()
          query1 = "INSERT INTO Users_Details VALUES('{E}','{P}',{N},'{A}')".format(E = email, P = password, N = number, A = address)
          cursor.execute(query1)
          connection.commit()
          return redirect("/home")
     
     return render_template("signup.html")

          

@app.route("/book", methods =["GET","POST"])
def book():
    if request.method == "POST":
        destinations = request.form["destinations"]
        time = request.form["time"]
        connection = sqlite3.connect(currentdirectory + "\database.db")
        cursor = connection.cursor()
        query1 = "INSERT INTO timeTable VALUES('{d}',{t})".format( d = destinations, t = time)
        cursor.execute(query1)
        connection.commit()
        return redirect("/confirmation")
     
    return render_template("book.html")

@app.route("/confirmation")
def confirmation():
    return render_template("confirmation.html")

if __name__ =="__main__":
    app.run(debug=True)

book.html代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vut website bus shuttle ticket booking</title>
    <link rel="stylesheet" href="/static/style.css">
</head>
<body>
    <div class="head">
        <img src="/static/images/logo.png" alt="vut logo">
        <h1>Book Your Ticket Below</h1>
        <h4>Contact Us: <br>064 304 5858</h4>
    </div>
        <form action="#" method ="POST" class="form-row"  enctype="multipart/form-data">
            <div class="select">
                <label>
                <h4>From Campus To:</h4>
                <select name="destinations" class="block" required>
                    <option class="choose">Choose A Res....</option>
                    <option>Amberfield</option>
                    <option>Park Village</option>
                    <option>Park Square</option>
                    <option>Good Year</option>
                    <option>SunTrust</option>
                    <option>GreenWays</option>
                </select>
                </label>

                    <h4>Time:</h4>
                <select name="time" class="block" required>
                    <option class="choose" selected disabled>Choose Departure Time</option>
                    <option>09:15</option>
                    <option>10:15</option>
                    <option>11:15</option>
                    <option>12:15</option>
                    <option>14:15</option>
                    <option>15:15</option>
                    <option>17:15</option>
                    <option>19:15</option>
                </select><br>
                
                <div class="block">
                  <input   type="submit"  class="book-btn" name="book" value="BOOK">
                </div>
            </div>
            
        </form>
            

    
    
</body>
</html>
ryevplcw

ryevplcw1#

成功登录“myhome”路由后,您将直接从该路由中显示book.html页面,而不是重定向到正确的“book”路由。这将正确显示页面,但url保持不变,因此数据被回发到“myhome”而不是“book”。将render_template(“book.html”)替换为redirect(url_for(“book”)),页面将显示在正确的url下,您的帖子应该可以正常工作。

相关问题