从mySQL数据库检索数据以进行会话

时间:2019-03-02 16:06:44

标签: python mysql database session

我目前正在制作python网络应用程序,以显示卫生中心的患者信息。我目前有一个登录系统,如下所示:

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        # Get form fields
        username = request.form['username']
        password_candidate = request.form['password']

        # Create cursor
        cur = mysql.connection.cursor()

        # Get user by username
        result = cur.execute("SELECT * FROM patients WHERE patient_username =%s", [username])

        if result > 0:
            # Get stored hash
            data = cur.fetchone()
            patient_password = data['patient_password']

            # Compare passwords
            if sha256_crypt.verify(password_candidate, patient_password):
                session['logged_in'] = True
                session['username'] = username

                flash('You are logged in', 'success')
                return redirect(url_for('dashboard'))

            else:
                error = 'Invalid login'
                return render_template('login.html', error=error)
            # Close connection
            cur.close()
        else:
            error = 'Username not found'
            return render_template('/patient/login.html', error=error)

    return render_template('/patient/login.html')

我想知道如何将数据库中的整个行放入会话中,以便能够检索数据并将其显示在表格中以显示患者详细信息等。任何帮助将不胜感激。谢谢

0 个答案:

没有答案