Python和Flask局部变量'光标'在分配之前引用

时间:2016-03-04 16:58:11

标签: 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()

如果有人可以帮我解决这个问题,我将非常感激。

1 个答案:

答案 0 :(得分:1)

我正在分配参数request.form别名错误。

有些别名正在获取request.form,右边会request.form