2016-03-04 55 views
-1

對不起,我的英語谷歌翻譯。在分配之前引用的Python和Flask局部變量'遊標'

我正在學習python和flask,我在函數中有一個問題。

@app.route('/addProperty',methods=['POST']) 
def addProperty(): 
    try: 
     if session.get('user'): 
      _tag = request.form['inputTag'] 
      _idCategoria = request.form['inputIdCategoria'] 
      _descricaoBem = request.form['inputDescricaoBem'] 
      _valor = request.form['inputValor'] 
      _fornecedor = request.form['inputFornecedor'] 
      _nfNumero = resquest.form['inputNfNumero'] 
      _nfSerie = request.form['inputNfSerie'] 
      _dtCompra = resquest.form['inputDtCompra'] 
      _departamento = request.form['inputDepartamento'] 
      _local = request.form['inputLocal'] 
      _responsavel = resquest.form['inputResponsavel'] 
      _estadoBem = request.form['inputEstadoBem'] 
      _dtUltimaRev = request.form['inputDtUltimaRev'] 
      _dtProximaRev = request.form['inputDtProximaRev'] 
      _idade = request.form['inputIdade'] 
      _vidaUtil = request.form['inputVidaUtil'] 
      _trocarReformar = request.form['inputTrocaReforma'] 
      _valorTT = request.form['inputValorTT'] 

      curDb.callproc('sp_addProperty',(_tag,_idCategoria,_descricaoBem,_valor,_fornecedor, 
               _nfNumero,_nfSerie,_dtCompra,_departamento,_local, 
               _responsavel,_estadoBem,_dtUltimaRev,_dtProximaRev, 
               _idade,_vidaUtil,_trocarReformar,_valorTT)) 
      conn = mysql.connect() 
      cursor = conn.cursor() 
      cursor.callproc('sp_addProperty',(_departamento,_user)) 
      data = cursor.fetchall() 

      if len(data) is 0: 
       conectaDb.commit() 
       return redirect('/userHome') 
      else: 
       return render_template('error.html',error = 'An error occurred!') 

     else: 
      return render_template('error.html',error = 'Unauthorized Access') 
    except Exception as e: 
     return render_template('error.html',error = str(e)) 
    finally: 
     cursor.close() 
     conn.close() 

該函數返回以下錯誤:

UnboundLocalError: local variable 'cursor' referenced before assignment 

和下面的功能是相同的結構,並且不返回錯誤。

@app.route('/addStatus',methods=['POST']) 
def addStatus(): 
    try: 
     if session.get('user'): 
      _status = request.form['inputStatus'] 
      _user = session.get('user') 

      conn = mysql.connect() 
      cursor = conn.cursor() 
      cursor.callproc('sp_addStatus',(_status,_user)) 
      data = cursor.fetchall() 

      if len(data) is 0: 
       conn.commit() 
       return redirect('/userHome') 
      else: 
       return render_template('error.html',error = 'An error occurred!') 

     else: 
      return render_template('error.html',error = 'Unauthorized Access') 
    except Exception as e: 
     return render_template('error.html',error = str(e)) 
    finally: 
     cursor.close() 
     conn.close() 

如果有人能幫我解決這個問題,我將不勝感激。

+2

有光標被分配在什麼地方出錯,所以finally塊引用光標時,它不存在。 – davidism

+0

這些字段最有可能不存在於發佈的數據中,並且Flask正在引發400錯誤,您通過自己實現錯誤頁面而不是使用Flask機制來隱藏該錯誤。 – davidism

+2

另外,使用像Flask-SQLAlchemy這樣的庫來處理連接,會話等。 – davidism

回答

0

我正在爲參數request.form分配不正確的別名。

一些別名漸漸的Request.Form其中權將REQUEST.FORM

相關問題