Django RestAuth自定义密码重置链接 [英] Django RestAuth Custom password reset link

查看:119
本文介绍了Django RestAuth自定义密码重置链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过针对类似问题找到的解决方案,但没有一个对我有用.

I have tried the solutions I found for a similar question but none worked for me.

我使用了一个有角度的前端+ DRF + Django Rest Auth,对于确认URL,我可以通过添加一个看起来像这样的自定义适配器来覆盖它以指向我的前端,

I am using an angular frontend + DRF + Django Rest Auth, for the confirmation url, I was able to override it to point to my frontend by adding a custom adapter that looks liked this,

class AccountAdapter(DefaultAccountAdapter):

    def send_mail(self, template_prefix, email, context):
        context['activate_url'] = settings.URL_FRONT + \
            'access/verify-email/' + context['key']
        msg = self.render_mail(template_prefix, email, context)
        msg.send()

使用URL_FRONT = 'http://localhost:8080/app/#/'作为将用户定向到客户端的设置.

with URL_FRONT = 'http://localhost:8080/app/#/' as the setting to direct the user to the client.

我的问题是对密码重置URL实施了同样的事情.我希望它从URL_FRONT设置开始,并附加令牌,就像我对确认的要求一样. 最好的方法是什么?

My problem is implementing the same thing for the password reset url. I want it to start with the URL_FRONT setting and attached the tokens just liked what I have for the confirmation. What will be the best way to go about this?

推荐答案

django-rest-auth似乎使用django.contrib.auth.forms.PasswordResetForm来处理此问题: https://github.com/Tivix/django -rest-auth/blob/master/rest_auth/serializers.py#L183

It looks like django-rest-auth uses django.contrib.auth.forms.PasswordResetForm to handle this: https://github.com/Tivix/django-rest-auth/blob/master/rest_auth/serializers.py#L183

并且根据 Django doc ,您可以传递自己的电子邮件模板(注意:Django 1.11进行了重大更改,因此请确保使用Django版本的doc).默认模板为registration/password_reset_form.html.您可以在

And according to the Django doc you can pass your own email template (note: there was a major change in Django 1.11, so make sure you use your Django version's doc). The default template is registration/password_reset_form.html. You can override this by using your own PasswordResetSerializer (extending rest_auth.serializers.PasswordResetSerializer) in the django-rest-auth configuration.

您可以在其中覆盖 get_email_options() 方法并设置自己的模板

There you can override the get_email_options() method and set your own template

def get_email_options(self):
    return {
        'email_template_name': 'path/to/your/txt_template.html',
        'html_email_template_name': 'path/to/your/html_template.html',
    }

或者仅仅将您的URL传递为success_url就足够了.

Or maybe it's simply enough to pass your URL as success_url.

这篇关于Django RestAuth自定义密码重置链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆