2017-10-08 150 views
0

我不确定为什么这段代码给了我一个与Flask的Web应用程序500错误,有人能告诉我我的逻辑错在哪里吗?我有一种感觉,它与sha256_crypt.verify()函数有关,但不确定如何解决这个问题。试图从我的表中拉出用户的所有数据并将其放入会话变量中。让我知道,谢谢!500错误MySQLdb Python从表中提取数据时

@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/ 
+1

异常从日志粘贴。 – jordanm

+0

完成,对不起,我不知道这是否是发布错误日志的正确方法lol – douglasrcjames

+0

您还没有设置mod_wsgi配置来告诉Python您的项目代码所在的位置。参见http://flask.pocoo.org/docs/0.12/deploying/mod_wsgi/ –

回答

0

我解决了这个问题一段时间回来,想更新万一别人运行到这个职位。

因此,我所做的只是将c.execute()函数从sha256_crpt IF语句中取出,并将它们放在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