如何通过更改密码的链接向新注册用户发送邮件?

时间:2017-10-12 14:56:40

标签: django

用户提交一份包含姓名,电子邮件等的表格。此表格已提交,并创建了用户帐户,并设置了随机密码。

如何向每位新用户发送一封邮件,其中包含直接更改 密码的链接;我的意思是没有 password_reset_form.html 步骤。

简明扼要地说,我希望用

发送邮件
{{ protocol }}://{{ domain }}{% url 'password_reset_confirm' uidb64=uid token=token %}

但我明白了:

django.urls.exceptions.NoReverseMatch: Reverse for 'password_reset_confirm' with keyword arguments '{'uidb64': '', 'token': ''}' not found. 
1 pattern(s) tried: ['accounts/reset/(?P<uidb64>[0-9A-Za-z_\\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$']

修改

传递令牌和uid后,邮件中的链接无效。

以下是我尝试将 uid 令牌传递给我的函数的方式:

def password_mail(sender, instance, **kwargs):
    if kwargs['created']:
        user_email = instance.email
        first_name = instance.first_name
        domain = Site.objects.get_current().domain
        uid = urlsafe_base64_encode(force_bytes(instance.pk))
        token = token_generator.make_token(instance)



        text_content = render_to_string('accounts/mail_password.txt', {'first_name': first_name, 'domain': domain, 'uid': uid, 'token': token})

我不再收到此错误,但提供的链接不是有效链接。

1 个答案:

答案 0 :(得分:0)

请将 uid 令牌作为上下文变量传递给上面给出的模板。