猿问

赋值前如何解决局部变量“密码”引用

我正在使用发布请求存储数据,但是每当我单击提交时,我的代码中都会出现此错误,提示“分配前局部变量“密码”引用”。我已经检查了所有的缩进。


@app.route('/admin/new_nurse', methods=['GET','POST'])

@login_required

def create_nurse(): 

    form=NurseForm()

    x=time.strftime("%H:%M:%S")

    if (x>=('00:00:00')and x<=('11:59:59')):

        x=(str('Good Morning'))

    elif x>=('12:00:00')and x<=('15:59:59'):

        x=(str('Good Afternoon'))

    elif x>=('16:00:00')and x<=('23:59:59'):

        x=(str('Good Evening'))


    if form.validate_on_submit():

        if form.picture.data:

            picture_file = save_picture(form.picture.data)

            firstname=request.form['fname']

            surname=request.form['sname']

            date_of_birth=request.form['dob']

            age_of_nurse=request.form['age']

            sex_option=request.form['sexoption']

            nurse_telephone=request.form['telnumber']

            educational_status=request.form['educationalstatus']

            Postal_Address=request.form['postaladdress']

            House_No=request.form['houseno']

            locality=request.form['locality']

            dept=request.form['dpt']

            username=form.username.data

            email=form.email.data

            password=form.password.data

            relative_info=request.form['relative']

        hashed_password= bcrypt.generate_password_hash(password).decode('utf-8')


慕码人2483693
浏览 139回答 2
2回答

Qyouu

也许您已经检查过这一点,但是如果form.validate_on_submit()返回 True 并且form.picture.data是 None 您最终会使用“密码”的值,但是,至少在您发布的代码中,您没有为其分配任何值。

慕哥9229398

如果if&nbsp;form.picture.data:为 False,您的密码变量将不会被初始化,这将导致上述错误行hashed_password=&nbsp;bcrypt.generate_password_hash(password).decode('utf-8')
随时随地看视频慕课网APP

相关分类

Python
我要回答