【问题标题】:Django how to generate an automatic activation key for email activation linkDjango如何为电子邮件激活链接生成自动激活密钥
【发布时间】:2023-03-03 01:43:01
【问题描述】:

我想知道如何生成自动激活密钥以连接到我的电子邮件激活链接 这是发送的链接:http://localhost:8001/sign_up/60/subsribe/14/confirmEmail 但为了更安全,我想发送类似:http://localhost:8001/sign_up/60/subsribe/14/confirmEmail/{{ activation_key }}

mail = EmailMultiAlternatives("Confirmation d\'email pour finaliser votre inscripttion","pour finaliser votre inscription, veuillez Consulter le Lien ci-dessus",from_email, to=[email]) mail.attach_alternative('http://localhost:8001/sign_up/' + str(id_account) + '/subscribe/' + str(id_sub) + '/confirmEmail',"text/html") 邮件发送()

【问题讨论】:

    标签: django email key activation


    【解决方案1】:

    您可以使用 Signer 类来实现这一点。即

    from django.core.signing import Signer
    signer = Signer()
    signed_value = signer.sign(profile.user.email)#gives 'email@email.com:signed_things', extract signed_things'
    key = ''.join(signed_value.split(':')[1:])
    #send out key as part of url
    

    然后,您可以将密钥与用户配置文件一起存储。当请求链接时,您可以执行以下操作:

    profile = get_object_or_404(UserProfile, key=key)
    signer = Signer()
    if signer.unsign('{0}:{1}'.format(profile.user.email, key)) == profile.user.email:
        profile.verified = True
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-12
      • 2011-02-14
      • 2021-04-28
      • 1970-01-01
      • 2015-03-24
      • 2014-07-24
      • 2014-02-01
      • 2012-10-06
      相关资源
      最近更新 更多