Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. In views.py, following from linked tutorial
  2.  
  3. class PasswordResetConfirmView(FormView):
  4. template_name = "registration/password_reset_confirm.html"
  5. success_url = 'registration/password_reset_complete.html'
  6. form_class = SetPasswordForm
  7.  
  8.  
  9. def post(self, request, uidb64=None, token=None, *arg, **kwargs):
  10. """
  11. View that checks the hash in a password reset link and presents a
  12. form for entering a new password.
  13. """
  14. UserModel = get_user_model()
  15. form = self.form_class(request.POST)
  16. assert uidb64 is not None and token is not None # checked by URLconf
  17. try:
  18. uid = urlsafe_base64_decode(uidb64)
  19. user = UserModel._default_manager.get(pk=uid)
  20. except (TypeError, ValueError, OverflowError,UserModel.DoesNotExist):
  21. user = None
  22.  
  23. if user is not None and default_token_generator.check_token(user,
  24. token):
  25. if form.is_valid():
  26. new_password= form.cleaned_data['new_password2']
  27. user.set_password(new_password)
  28. user.save()
  29. messages.success(request, 'Password has been reset.')
  30. return self.form_valid(form)
  31. else:
  32.  
  33. messages.error(request, 'Password reset has not been
  34. unsuccessful.')
  35. return self.form_invalid(form)
  36. else:
  37. messages.error(request,'The reset password link is no longevalid.')
  38. return self.form_invalid(form)```
  39.  
  40.  
  41. In urls.py
  42.  
  43.  
  44. url(r'^account/password_reset/', ResetPasswordRequestView.as_view(),
  45. name="reset_password"),
  46. url(r'^account/password/reset/done/', ResetPasswordRequestView.as_view(),
  47. name="reset_password_done"),
  48. url(r'^reset/(?P<uidb64>[0-9A-Za-z]+)/(?P<token>.+)/$',
  49. PasswordResetConfirmView.as_view(),name='password_reset_confirm'),
  50.  
  51. In password_reset_confirm.html in the Registration folder
  52.  
  53. {% extends 'base.html' %}
  54.  
  55.  
  56.  
  57. {% block title %}Enter new password{% endblock %}
  58. {% block content %}
  59. <h1>Set a new password!</h1>
  60. <form method="POST">
  61. {% csrf_token %}
  62. {{ form.as_p }}
  63. <input type="submit" value="Change my password">
  64. </form>
  65. {% endblock %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement