djoser的密码重置实现 [英] password reset implementation with djoser

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

问题描述

我想使用 djoser 来实现重置密码功能,并按照文档进行操作:

I wanted to use djoser for the reset password functionality and as per the documentation:


PASSWORD_RESET_CONFIRM_URL

您的前端密码重置页面的URL。它应包含{uid}和
{token}占位符,例如#/密码重置/ {uid} / {token} 。您应该
传递uid和令牌来重置密码确认端点。

URL to your frontend password reset page. It should contain {uid} and {token} placeholders, e.g. #/password-reset/{uid}/{token}. You should pass uid and token to reset password confirmation endpoint.

我已经执行了以下操作:

I have done the following:

PASSWORD_RESET_CONFIRM_URL':重置/密码/重置/确认/ {uid} / {token},

url

url(r'^ reset / password / reset / confirm /(?P< uid> [\w-] +)/(?P< token> [\w-] +)/ $',PasswordResetView.as_view(),),

View:

class PasswordResetView(APIView):

   def get (self, request, uid, token):
       post_data = {'uid': uid, 'token': token}
       return Response(post_data)

在我的邮件中,我获得此链接: http://127.0.0.1:8000/reset/password/reset/confirm/Mjk/ 538-954dccbc1b06171eff4d

In my mail I get this link : http://127.0.0.1:8000/reset/password/reset/confirm/Mjk/538-954dccbc1b06171eff4d

很明显,我会得到:

{
"uid": "Mjk",
"token": "538-954dccbc1b06171eff4d"

}

作为我的输出,但是当用户单击邮件中的链接时,我想转到 auth / password / reset / confirm

as my output but I wanted to go to auth/password/reset/confirm when the user clicks the link in the mail.

推荐答案

让我们首先描述操作:


  1. 用户单击链接重设密码。 重置密码

  2. (在这里需要一个表格来获取用户名或电子邮件地址,具体取决于您的设置。)用户输入用户名,然后单击提交。

  3. 用户收到一封电子邮件,其中包含用于重置密码的链接。

  4. 该链接将打开浏览器,其中包含创建新密码的形式。

  5. 用户输入新密码并发送表单

  6. 浏览器将页面重定向到主页,并提供密码已重置的反馈

  1. The user clicks on the link to reset the password. reset password
  2. (Here you need a form to obtain a username or email address, depending on your settings) The user enters the username and clicks Submit.
  3. The user receives an email with a link to reset the password.
  4. The link opens the browser, which contains the form "Create a new password".
  5. The user enters a new password and sends a form
  6. The browser redirects the page to the home page and gives feedback that the password has been reset.






然后您可以使用以下方法重设密码。


You can then use following method to reset the password.

#template
<p>Use the form below to change your password. Your password cannot be the same as your username.</p>
<form role="form" method="post">
  {% csrf_token %}
  <input type="password" name="password1" placeholder="New Password">
  <input type="submit">
</form>







#view
from django.shortcuts import redirect, render
from djoser.conf import django_settings

def reset_user_password(request, uid, token):
    if request.POST:
        password = request.POST.get('password1')
        payload = {'uid': uid, 'token': token, 'new_password': password}

        url = '{0}://{1}{2}'.format(
            django_settings.PROTOCOL, django_settings.DOMAIN, reverse('password_reset_confirm'))

        response = requests.post(url, data=payload)
        if response.status_code == 204:
            # Give some feedback to the user. For instance:
            # https://docs.djangoproject.com/en/2.2/ref/contrib/messages/
            messages.success(request, 'Your password has been reset successfully!')
            return redirect('home')
        else:
            return Response(response.json())
    else:
        return render(request, 'templates/reset_password.html')

这篇关于djoser的密码重置实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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