本文介绍了TypeError:必须先对Unicode对象进行编码,然后再进行哈希处理,从而创建用于激活电子邮件的哈希键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图创建为hash_key来激活电子邮件,但遇到上述类型错误.我如何实施编码的任何想法.下面是我的代码:
I am trying to create as hash_key for email activation but am getting the type error above. Any ideas how i can enforce encoding. Below is my code:
def user_created(sender, instance, created, *args, **Kwargs):
user = instance
if created:
get_create_stripe(user)
email_confirmed, email_is_created = EmailConfirmed.objects.get_or_create(user=user)
if email_is_created:
short_hash = hashlib.sha1(str(random.random()).hexdigest())[:5]
base, domain = str(user.email).split('@')
activation_key = hashlib.sha1(short_hash+base).hexdigest()
email_confirmed.activation_key = activation_key
email_confirmed.save()
email_confirmed.activate_user_email()
post_save.connect(user_created, sender=User)
推荐答案
您需要将 bytes
传递给 hashlib.sha1()
方法,而不是 string
.为此,您可以使用 encode()
像这样:
You need to pass bytes
to hashlib.sha1()
method instead of string
. To do it you can just use encode()
like this:
short_hash = hashlib.sha1(str(random.random()).encode()).hexdigest()[:5]
base, domain = str(user.email).split('@')
activation_key = hashlib.sha1((short_hash+base).encode()).hexdigest()
这篇关于TypeError:必须先对Unicode对象进行编码,然后再进行哈希处理,从而创建用于激活电子邮件的哈希键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!