Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

In django-allauth the messages are stored as txt files in django-allauth/allauth/templates/account/messages/.

You can copy the password_changed.txt file into your templates/account/messages/and customize the following code:

{% load i18n %}
{% blocktrans %}Password successfully changed.{% endblocktrans %}

Link to the password_changed.txt file on github

A nice simple way is to add this piece of code into the template password_change.html

<h1>{% translate "Change Password" %}</h1>   
{% if messages %}
  {% for message in messages %}
    {% if message.tags == 'success' %}          
      {% translate 'Password successfully changed.' %}
    {% endif %}
  {% endfor %}
{% endif %}   
<form method="POST" ...>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top