1

我有一个 Flask API,我在其中确认注册帐户。Itsdangerous 用于生成我通过电子邮件发送给用户的令牌。单击令牌后,API 会检查令牌是否有效并检查用户的电子邮件地址是否存在。当我单击电子邮件中的链接时,我收到以下错误:

UnboundLocalError: local variable 'email' referenced before assignment

如果我在确认端点函数中对电子邮件进行硬编码,它就可以完美运行。不知道我哪里错了。

这是发送确认电子邮件的代码:

def send_confirmation_email(user_email):
  confirm_serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])

  confirm_url = url_for(
    'users.confirm_email',
    token=confirm_serializer.dumps(user_email, salt='email-confirmation-salt'),
    _external=True)

  html = render_template(
    'email_confirmation.html',
    confirm_url=confirm_url)

  # Send account confirmation email
  mail.send_email(
    from_email='app@appdomain.com',
    to_email=user_email,
    subject='Confirm Your Email Address',
    text='Your account on AppName was successfully created. Please click the link below to confirm your email address and activate your account: '+confirm_url+'. If you have any problems email support@appdomain.com.'
  )

这是确认端点:

@user_api.route('/confirm/<token>')
def confirm_email(token):
  try:
    confirm_serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
    email = confirm_serializer.loads(token, salt='email-confirmation-salt', max_age=3600)
  except:
    flash('The confirmation link is invalid or has expired.', 'error')
    return redirect('http://localhost:3000/auth/login')

  user = UserModel.query.filter_by(email=email).first()

  if user.email_confirmed:
    return custom_response('Account already confirmed. Please login.', 200)
  else:
    user.email_confirmed = True
    user.email_confirmed_on = datetime.now()
    db.session.add(user)
    db.session.commit()

    return custom_response('Thank you for confirming your email address!', 200)

编辑 - 追溯:

File "/Users/derek/.local/share/virtualenvs/app-api-61-oxftK/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/derek/.local/share/virtualenvs/app-api-61-oxftK/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "/Users/derek/.local/share/virtualenvs/app-api-61-oxftK/lib/python3.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/Users/derek/.local/share/virtualenvs/app-api-61-oxftK/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/derek/.local/share/virtualenvs/app-api-61-oxftK/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/derek/.local/share/virtualenvs/app-api-61-oxftK/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/Users/derek/.local/share/virtualenvs/app-api-61-oxftK/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/derek/.local/share/virtualenvs/app-api-61-oxftK/lib/python3.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/Users/derek/.local/share/virtualenvs/app-api-61-oxftK/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/derek/.local/share/virtualenvs/app-api-61-oxftK/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/derek/.local/share/virtualenvs/app-api-61-oxftK/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/derek/.local/share/virtualenvs/app-api-61-oxftK/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/derek/CodeRepos/App/app-api/src/users/users_api.py", line 198, in confirm_email
user = UserModel.query.filter_by(email=email).first()
UnboundLocalError: local variable 'email' referenced before assignment
4

0 回答 0