我不确定为什么这段代码会在Flask的Web应用程序上给我500错误,有人可以告诉我我的逻辑错在哪里吗?我觉得它与sha256_crypt.verify()函数有关,但不确定如何解决此问题。只是试图从我的表中提取用户的所有数据,然后将其放入会话变量中。让我知道,谢谢!

@app.route('/login/', methods=['GET','POST'])
def login_page():
    error = ''
    try:
        c, conn = connection()
        if request.method == "POST":
            c.execute("SELECT * FROM clients WHERE email = (%s)", (thwart(request.form['email']),))
            pdata = c.fetchone()[3]

            if sha256_crypt.verify(request.form['password'], pdata):
                #get info from clients table
                c.execute("SELECT * FROM clients WHERE email = (%s)", (email,))
                clients_results = fetchall()
                clientcid = clients_results[0]
                #get info from cpersonals table
                c.execute("SELECT * FROM cpersonals WHERE cid = (%s)", (clientcid,))
                cpersonals_results = fetchall()

                conn.commit()
                c.close()
                conn.close()


                session['logged_in'] = 'client'
                session['clientcid'] = clientcid
                session['email'] = email
                session['phone'] = clients_results[1]
                session['rating'] = clients_results[4]
                session['first_name'] = cpersonals_results[1]
                session['last_name'] = cpersonals_results[2]
                session['address'] = cpersonals_results[3]
                session['czip'] = cpersonals_results[4]
                session['reg_date'] = cpersonals_results[5]
                flash("You are now logged in.")
                return redirect(url_for("ask"))

            else:
                error = "Invalid credentials, try again."

        return render_template("login.html", error = error)

    except Exception as e:
        #flash(e)
        error = "Invalid credentials, try again."
        return render_template("login.html", error = error)


日志:(很抱歉,不确定如何正确发布)

   .991239 mod_wsgi (pid=5585): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module., referer: http://138.197.210.172/view_unanswered/
 .948625   mod_wsgi (pid=5585): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'., referer: http://138.197.210.172/view_unanswered/
 .948649   Traceback (most recent call last):, referer: http://138.197.210.172/view_unanswered/
 .948670     File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in <module>, referer: http://138.197.210.172/view_unanswered/
 .948744       from FlaskApp import app as application, referer: http://138.197.210.172/view_unanswered/
 .948857     File "/var/www/FlaskApp/FlaskApp/__init__.py", line 270, referer: http://138.197.210.172/view_unanswered/
 .948869       conn.commit(), referer: http://138.197.210.172/view_unanswered/
 .948873                   ^, referer: http://138.197.210.172/view_unanswered/
 .948877   IndentationError: unindent does not match any outer indentation level, referer: http://138.197.210.172/view_unanswered/
06.003866   mod_wsgi (pid=5586): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module., referer: http://138.197.210.172/account/
06.003919   mod_wsgi (pid=5586): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'., referer: http://138.197.210.172/account/
06.003942   Traceback (most recent call last):, referer: http://138.197.210.172/account/
06.003964     File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in <module>, referer: http://138.197.210.172/account/
06.004030       from FlaskApp import app as application, referer: http://138.197.210.172/account/
06.004076     File "/var/www/FlaskApp/FlaskApp/__init__.py", line 270, referer: http://138.197.210.172/account/
06.004083       conn.commit(), referer: http://138.197.210.172/account/
06.004087                   ^, referer: http://138.197.210.172/account/
06.004090   IndentationError: unindent does not match any outer indentation level, referer: http://138.197.210.172/account/
09.513219   mod_wsgi (pid=5585): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module., referer: http://138.197.210.172/
09.513258   mod_wsgi (pid=5585): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'., referer: http://138.197.210.172/
09.513280   Traceback (most recent call last):, referer: http://138.197.210.172/
09.513301     File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in <module>, referer: http://138.197.210.172/
09.513335       from FlaskApp import app as application, referer: http://138.197.210.172/
09.513357   ImportError: cannot import name app, referer: http://138.197.210.172/
09.550576   mod_wsgi (pid=5586): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module., referer: http://138.197.210.172/view_unanswered/
09.550615   mod_wsgi (pid=5586): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'., referer: http://138.197.210.172/view_unanswered/
09.550639   Traceback (most recent call last):, referer: http://138.197.210.172/view_unanswered/
09.550661     File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in <module>, referer: http://138.197.210.172/view_unanswered/
09.550697       from FlaskApp import app as application, referer: http://138.197.210.172/view_unanswered/
09.550718   ImportError: cannot import name app, referer: http://138.197.210.172/view_unanswered/
12.922623  mod_wsgi (pid=5585): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module., referer: http://138.197.210.172/
12.922697  mod_wsgi (pid=5585): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'., referer: http://138.197.210.172/
12.922743  Traceback (most recent call last):, referer: http://138.197.210.172/
12.922768    File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in <module>, referer: http://138.197.210.172/
12.922807      from FlaskApp import app as application, referer: http://138.197.210.172/
12.922838  ImportError: cannot import name app, referer: http://138.197.210.172/
12.955475   mod_wsgi (pid=5586): Target WSGI script '/var/www/FlaskApp/flaskapp.wsgi' cannot be loaded as Python module., referer: http://138.197.210.172/
12.955537   mod_wsgi (pid=5586): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'., referer: http://138.197.210.172/
12.955561   Traceback (most recent call last):, referer: http://138.197.210.172/
12.955584     File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in <module>, referer: http://138.197.210.172/
12.955620       from FlaskApp import app as application, referer: http://138.197.210.172/
12.955641   ImportError: cannot import name app, referer: http://138.197.210.172/

最佳答案

我将其修复了一会儿,并希望更新该帖子,以防其他人遇到该帖子。

因此,我所做的就是从c.execute() IF语句中删除sha256_crpt函数,并将它们放在login重定向到的页面的函数开头。每个用户都访问第一页,因此用户会话将始终具有这些值。

因此:

@app.route('/ask/', methods=['GET','POST'])
def ask():
    #clients come here first
    error = ''
    try:
        c, conn = connection()
        #grab all the clients info
        email = session['email']
        c.execute("SELECT cid FROM clients WHERE email = (%s)", (thwart(email),))
        clientcid = c.fetchone()[0]
        c.execute("SELECT phone FROM clients WHERE email = (%s)", (thwart(email),))
        phone = c.fetchone()[0]
        c.execute("SELECT rating FROM clients WHERE email = (%s)", (thwart(email),))
        rating = c.fetchone()[0]
        c.execute("SELECT first_name FROM cpersonals WHERE cid = (%s)", (clientcid,))
        first_name = c.fetchone()[0]
        c.execute("SELECT last_name FROM cpersonals WHERE cid = (%s)", (clientcid,))
        last_name = c.fetchone()[0]
        c.execute("SELECT address FROM cpersonals WHERE cid = (%s)", (clientcid,))
        address = c.fetchone()[0]
        c.execute("SELECT city FROM cpersonals WHERE cid = (%s)", (clientcid,))
        city = c.fetchone()[0]
        c.execute("SELECT state FROM cpersonals WHERE cid = (%s)", (clientcid,))
        state = c.fetchone()[0]
        c.execute("SELECT zip FROM cpersonals WHERE cid = (%s)", (clientcid,))
        czip = c.fetchone()[0]
        c.execute("SELECT reg_date FROM cpersonals WHERE cid = (%s)", (clientcid,))
        reg_date = c.fetchone()[0]
        conn.commit()
        c.close()
        conn.close()


        session['logged_in'] = 'client'
        session['clientcid'] = clientcid
        session['phone'] = phone
        session['rating'] = rating
        session['first_name'] = first_name
        session['last_name'] = last_name
        session['address'] = address
        session['city'] = city
        session['state'] = state
        session['czip'] = czip
        session['reg_date'] = reg_date

关于python - 从表中提取数据时,MySQLdb Python出现500错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46626619/

10-11 20:53