为什么我的模块烧瓶上出现此 KeyError?[关闭]

why i get this KeyError on my module flask? [closed]

提问人:nickfreeman 提问时间:11/15/2023 更新时间:11/15/2023 访问量:33

问:


编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将帮助其他人回答这个问题。

4天前关闭。

我还是新手,我收到这个错误“ BadRequestKeyError werkzeug.exceptions.BadRequestKeyError:400 错误请求:浏览器(或代理)发送了此服务器无法理解的请求。 KeyError: 'judul'”

我真的希望有人能帮助我。我无法将“Mata Kuliah”保存到数据库中,但“Mahasiswa”很好。只有Mata Kuliah有问题。

我已经确保“judul”已经添加到数据库的表中。顺便说一句,对不起我的语法,英语是我的第二语言。有人可以帮我吗?我真的希望有人能指出我在这个代码中哪里出了问题。我已经问过我的教授,但他说我应该先自己解决。顺便说一句,谢谢你

无论如何,这是我的代码

from flask import Flask, render_template, request, redirect, url_for, flash, session
from flask_mysqldb import MySQL

app = Flask(__name__)

# MySQL Configuration
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'dbpolibatam'
mysql = MySQL(app)

# Secret Key
app.secret_key = "4cc645e832bc2ed0869da6d3a9bdc0ea"

@app.route('/', methods=['GET'])
def login():
    return render_template('login.html')

@app.route('/verifikasi-login', methods=['POST'])
def verify_login():
    if request.method == 'POST':
        email = request.form['email']
        password = request.form['password']

        cur = mysql.connection.cursor()
        cur.execute('SELECT * FROM users WHERE email = %s AND password = %s', (email, password))
        user = cur.fetchone()
        cur.close()

        if user:
            user_session_data = {'id': user[0], 'email': user[1]}
            session['user_id'] = user_session_data['id']
            flash('Login successful. Welcome!', 'success')
            return redirect('/dashboard')
        else:
            flash('Incorrect email or password', 'error')
            return redirect('/')

@app.route('/logout')
def logout():
    session.clear()
    return redirect('/')

@app.route('/dashboard', methods=['GET'])
def dashboard():
    if 'user_id' in session:
        return render_template('dashboard.html')
    else:
        return redirect(url_for('login'))

# Mahasiswa (Students)
@app.route('/mahasiswa', methods=['GET'])
def data_mahasiswa():
    cur = mysql.connection.cursor()
    cur.execute('SELECT id, nim, nama_lengkap, alamat FROM students')
    data = cur.fetchall()
    cur.close()
    return render_template('mahasiswa/data-mahasiswa.html', mahasiswa=data)

@app.route('/mahasiswa/tambah', methods=['GET'])
def tambah_mahasiswa():
    return render_template('mahasiswa/tambah-mahasiswa.html')

@app.route('/mahasiswa/insert', methods=['POST'])
def add_mahasiswa():
    if request.method == 'POST':
        nim = request.form['nim']
        nama_lengkap = request.form['nama_lengkap']
        alamat = request.form['alamat']
        cur = mysql.connection.cursor()
        cur.execute("INSERT INTO students (nim, nama_lengkap, alamat) VALUES (%s, %s, %s)",
                    (nim, nama_lengkap, alamat))
        mysql.connection.commit()
        flash('Student data added successfully!')
        return redirect(url_for('data_mahasiswa'))

@app.route('/mahasiswa/edit/<int:id>', methods=['GET'])
def edit_mahasiswa(id):
    cur = mysql.connection.cursor()
    cur.execute("SELECT * FROM students WHERE id = %s", (id,))
    data = cur.fetchall()
    cur.close()
    print(data[0])
    return render_template('mahasiswa/ubah-mahasiswa.html', contact=data[0])

@app.route('/mahasiswa/update/<int:id>', methods=['POST'])
def update_mahasiswa(id):
    if request.method == 'POST':
        nim = request.form['nim']
        nama_lengkap = request.form['nama_lengkap']
        alamat = request.form['alamat']
        cur = mysql.connection.cursor()
        cur.execute("UPDATE students SET nim = %s, nama_lengkap = %s, alamat = %s WHERE id = %s",
                    (nim, nama_lengkap, alamat, id))
        flash('Student data updated successfully!')
        mysql.connection.commit()
        return redirect(url_for('data_mahasiswa'))

@app.route('/mahasiswa/delete/<int:id>', methods=['POST', 'GET'])
def delete_mahasiswa(id):
    cur = mysql.connection.cursor()
    cur.execute('DELETE FROM students WHERE id = %s', (id,))
    mysql.connection.commit()
    flash('Student data deleted successfully!')
    return redirect(url_for('data_mahasiswa'))

# Mata Kuliah (Courses)
@app.route('/matkul', methods=['GET'])
def data_matkul():
    cur = mysql.connection.cursor()
    cur.execute('SELECT kode, judul, deskripsi, sks, program_studi FROM courses')
    data = cur.fetchall()
    cur.close()
    return render_template('matkul/data-matkul.html', matkul=data)

@app.route('/matkul/tambah', methods=['GET'])
def tambah_matkul():
    return render_template('matkul/tambah-matkul.html')

@app.route('/matkul/insert', methods=['POST'])
def add_matkul():
    if request.method == 'POST':
        judul = request.form['judul']
        deskripsi = request.form['deskripsi']
        sks = request.form['sks']
        program_studi = request.form['program_studi']
        cur = mysql.connection.cursor()
        cur.execute("INSERT INTO courses (judul, deskripsi, sks, program_studi) VALUES (%s, %s, %s, %s)",
                    (judul, deskripsi, sks, program_studi))
        mysql.connection.commit()
        flash('Course data added successfully!')
        return redirect(url_for('data_matkul'))

@app.route('/matkul/edit/<int:id>', methods=['GET'])
def edit_matkul(id):
    cur = mysql.connection.cursor()
    cur.execute("SELECT * FROM courses WHERE kode = %s", (id,))
    data = cur.fetchall()
    cur.close()
    print(data[0])
    return render_template('mahasiswa/ubah-matkul.html', contact=data[0])

@app.route('/matkul/update/<int:id>', methods=['POST'])
def update_matkul(id):
    if request.method == 'POST':
        judul = request.form['judul']
        deskripsi = request.form['deskripsi']
        sks = request.form['sks']
        program_studi = request.form['program_studi']
        cur = mysql.connection.cursor()
        cur.execute("""
            UPDATE courses
            SET judul = %s, deskripsi = %s, sks = %s, program_studi = %s
            WHERE kode = %s
        """, (judul, deskripsi, sks, program_studi, id))
        flash('Course data updated successfully!')
        mysql.connection.commit()
        return redirect(url_for('data_matkul'))

@app.route('/matkul/delete/<int:id>', methods=['POST', 'GET'])
def delete_matkul(id):
    cur = mysql.connection.cursor()
    cur.execute('DELETE FROM courses WHERE kode = %s', (id,))
    mysql.connection.commit()
    flash('Course data deleted successfully!')
    return redirect(url_for('data_matkul'))

# Run the application on port 9999
if __name__ == "__main__":
    app.run(port=9999, debug=True)

请帮帮我,这是我的学校项目

python mysql 烧瓶 flash

评论

0赞 roganjosh 11/15/2023
您实际上没有显示错误和回溯,也没有对问题所在进行任何明确的描述。我们不能只为你浏览数百行代码 - 我们需要一个最小的可重现示例

答:

0赞 programORdie 11/15/2023 #1

此错误意味着您尝试获取不存在的表单,例如 但在 html 中。Flask 无法访问表单,但只有表单存在,因此它将返回错误。确保所有内容都与烧瓶后端相同。request.form['mail']<input name="email">mailemailinput names