pythondjangopassword-recoverydjango-allauth

How to add a success message after django allauth change password view has returned response


I am using django allauth to my django project for all my authentication related functionality, so now I want to implement password change functionality, so just copied the django allauth templates to my templates/allauth/account/password_change.html and customized with my custom design and has the form something like below

<form accept-charset="UTF-8" class="" action="{% url 'account_change_password' %}" method="post">
     {% csrf_token %}
     <div class="alert alert-success  password_changed">
          You have Successfully changed your Password!
     </div>
     {{form.as_p}} 
     <div class="span12 pagination-centered marg_lftnone">
          <input id="save_new_password" name="action" type="submit" class="btn btn-large big_btn marg_tp38 marg_btm38" value="Save Password">
     </div>
</form>   

So with the above template the password changing functionality has been working fine and redirecting to current page, but what I want is when redirected to current page, I want to show a message div like above about informing that you have changed password successfully.

So how to display and error message after the password has been changed successfully and redirected to the same page?


Solution

  • Allauth emits password_changed signal, so you need to hook up a receiver. In your models.py add the following:

    from allauth.account.signals import password_changed
    from django.dispatch import receiver
    from django.contrib import messages
    
    @receiver(password_changed)
    def password_change_callback(sender, request, user, **kwargs):
        messages.success(request, 'You have Successfully changed your Password!.')
    

    Then use your message inside template as documented here.