Python 如何为每个对象在Django中将变量从类传递到方法
我正在使用Python 如何为每个对象在Django中将变量从类传递到方法,python,django,class,one-time-password,Python,Django,Class,One Time Password,我正在使用django手动创建OTP验证器。我已经创建了一个类,它创建了一个otp并通过电子邮件发送给用户。我还没有编写发送电子邮件的代码。我将通过send\u email()内置函数来完成它。请查看views.py中的代码,我想使用另一个函数来验证otp。但一旦我在第二个函数中创建了相同的对象,它就会重新初始化变量 def register(request): """ Other stuffs like password matching goes here &
django
手动创建OTP验证器。我已经创建了一个类,它创建了一个otp并通过电子邮件发送给用户。我还没有编写发送电子邮件的代码。我将通过send\u email()
内置函数来完成它。请查看views.py
中的代码,我想使用另一个函数来验证otp。但一旦我在第二个函数中创建了相同的对象,它就会重新初始化变量
def register(request):
""" Other stuffs like password matching goes here """
""" This will be provoked first in production """
g=globals()
g["user" + str(request.POST['username'])] = OTPclass()
g["user" + str(request.POST['username'])].send_otp()
def verify(request):
""" This method will be provoked once the user enters the OTP received in the email """
g=globals()
g["user" + str(request.POST["username"])] = OTPclass()
#In this part the value reinitializes to 0, but I must get the otp which was sent to the user
if(int(request.POST['otp']) == g["user" + str(request.POST["username"])].the_otp):
return redirect(login)
else:
print(g["user" + str(request.POST["username"])].the_otp)
return HttpResponse("<html><body><h2>OTP mismatch</h2></body></html>")
class OTPclass:
the_otp = 0
def send_otp(self):
self.the_otp = random.randint(1000,9999)
""" send_email() will be used here to send the otp to the user """
def寄存器(请求):
“”“其他内容,如密码匹配,请点击此处”“”
“”“这将首先在生产中引发”“”
g=全局()
g[“user”+str(request.POST['username'])]=OTPclass()
g[“用户”+str(request.POST['username'])。发送
def验证(请求):
“”“一旦用户输入在电子邮件中收到的OTP,就会触发此方法”“”
g=全局()
g[“user”+str(request.POST[“username”])]=OTPclass()
#在这部分中,值重新初始化为0,但我必须获取发送给用户的otp
如果(int(request.POST['otp'])==g[“user”+str(request.POST[“username”])。otp):
返回重定向(登录)
其他:
打印(g[“用户”+str(request.POST[“用户名]))]。otp)
返回HttpResponse(“OTP不匹配”)
类别OTP类别:
_otp=0
def发送otp(自我):
self.the_otp=random.randint(10009999)
“”“send_email()将在此处用于向用户发送otp”“”
请建议一种获取在
verify()
中发送给用户的值的方法。在视图.py中全局声明变量会导致在多个用户访问函数时覆盖该值。我不喜欢django,但我在Flask中也遇到了同样的问题。
您可以使用会话将数据从一个请求保存到另一个请求。
这可能是一个解决方案
另一个想法是在课堂上记住OTP。(如果不想使用数据库)
OTP=OTPclass()
def寄存器(请求):
“”“其他内容,如密码匹配,请点击此处”“”
“”“这将首先在生产中引发”“”
OTP.send_OTP(“用户”+str(request.POST['username']))
def验证(请求):
“”“一旦用户输入在电子邮件中收到的OTP,就会触发此方法”“”
如果(OTP.check_OTP(“用户”+str(request.POST[“用户名”])、int(request.POST[“OTP”]):
返回重定向(登录)
其他:
返回HttpResponse(“OTP不匹配”)
类别OTP类别:
备忘录={}
def发送otp(自身、用户):
self.memo[user]=random.randint(10009999)
“”“send_email()将在此处用于向用户发送otp”“”
def检查otp(自身、用户、密码):
如果备忘录中的用户和备忘录[用户]==密码:
返回真值
其他:
返回错误
请记住,此解决方案仅适用于一个线程/进程。将OTP存储在数据库中,即为其创建模型,或将其存储在会话中。
# set password:
request.session['one_time_password'] = 'secret'
# check password:
if request.session.get('one_time_password', False):
if request.session['one_time_password'] == provided_password:
# login
OTP = OTPclass()
def register(request):
""" Other stuffs like password matching goes here """
""" This will be provoked first in production """
OTP.send_otp("user" + str(request.POST['username']))
def verify(request):
""" This method will be provoked once the user enters the OTP received in the email """
if(OTP.check_otp("user" + str(request.POST["username"]),int(request.POST['otp']):
return redirect(login)
else:
return HttpResponse("<html><body><h2>OTP mismatch</h2></body></html>")
class OTPclass:
memo = {}
def send_otp(self, user):
self.memo[user] = random.randint(1000,9999)
""" send_email() will be used here to send the otp to the user """
def check_otp(self,user, password):
if user in memo and memo[user] == password:
return True
else:
return False