我在twilio的项目中包含了OTP。但是,当验证电话号码时,会引发以下错误。
phone.verified = True
AttributeError: 'str' object has no attribute 'verified'
models.py
class User(AbstractBaseUser, PermissionsMixin):
phone = models.CharField(max_length=15, unique=True)
is_active = models.BooleanField(default = True)
is_staff = models.BooleanField(default = False)
USERNAME_FIELD = 'phone'
key = models.CharField(max_length=100, unique=True, blank=True)
verified = models.BooleanField(default=False)
objects = UserManager()
views.py
@api_view(['GET'])
def send_sms_code(request, format=None):
time_otp = pyotp.TOTP(request.user.key, interval=10000)
time_otp = time_otp.now()
user_phone_number = request.user.phone
client.messages.create(
body="Your verification code is "+time_otp,
from_=twilio_phone,
to=user_phone_number
)
return Response(status=200)
@api_view(['GET'])
def verify_phone(request, sms_code, format=None):
code = int(sms_code)
if request.user.authenticate(code):
phone = request.user.phone
phone.verified = True
phone.save()
return Response(dict(detail = "Phone number verified successfully"),status=201)
return Response(dict(detail='The provided code did not match or has expired'),status=200)
它向我发送了验证码,但是当我要验证时,它无法正常工作。我知道问题出在哪里,但如何解决?任何帮助,将不胜感激!预先感谢!
答案 0 :(得分:2)
您的api视图将phone = statusCode
设置为字符串。然后,通过访问电话上的已验证属性,将电话当作request.user.phone
对象使用。相反,您的视图应为:
User
您似乎甚至没有使用phone变量,因此您提供给我们的信息实际上并不需要它。