【问题标题】:Django RestAuth Custom password reset linkDjango RestAuth 自定义密码重置链接
【发布时间】:2018-04-07 08:07:42
【问题描述】:

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

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

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/#/' 作为将用户引导至客户端的设置。

我的问题是对密码重置 URL 实施相同的操作。我希望它以URL_FRONT 设置开始,并附加了我所拥有的用于确认的令牌。 解决此问题的最佳方法是什么?

【问题讨论】:

  • 重置密码现在显示在哪里?
  • 它显示了 django 站点的 url。所以在localhost上,它是localhost:8000,我希望它显示http://localhost:8080/app/#/作为url

标签: angularjs django django-rest-framework


【解决方案1】:

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

根据Django doc,您可以传递自己的电子邮件模板(注意:Django 1.11 中有一个重大变化,因此请确保使用您的 Django 版本的文档)。默认模板为registration/password_reset_form.html。您可以在django-rest-auth configuration 中使用您自己的PasswordResetSerializer(扩展rest_auth.serializers.PasswordResetSerializer)来覆盖它。

在那里你可以覆盖get_email_options() 方法并设置你自己的模板

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 就足够了。

【讨论】:

  • 这行得通,当我使用不同的密码序列化程序时,我错过了 get_email_options 部分
  • 答案非常好,但是当我出于某种原因这样做时,请求是无:/并且我不发送任何上下文
【解决方案2】:

接受的答案有效,但我还在管理仪表板中使用前端 url 作为站点,它对我的​​所有 url 都非常有效

【讨论】:

  • 你能进一步解释一下吗?
  • 我将数据库中的站点名称更新为我的客户 url 的域。例如,如果客户端的 url 是 app.example.com,则在您的数据库上更新它或创建一个新站点并设置您的 SITE_ID=2
【解决方案3】:

settings.py中定义这些变量

  URL_FRONT = 'http://localhost:8080/app/#/'
  ACCOUNT_EMAIL_CONFIRMATION_URL = URL_FRONT+ 'verify-email/{}'
  ACCOUNT_PASSWORD_RESET_CONFIRM = URL_FRONT+ 'password-reset/confirm/'

【讨论】:

猜你喜欢
  • 2020-08-16
  • 2019-01-16
  • 2016-09-23
  • 1970-01-01
  • 2015-12-12
  • 1970-01-01
  • 1970-01-01
  • 2020-05-18
  • 2020-10-06
相关资源
最近更新 更多