Home > database >  How to detect change in HTML page after a successful redirect from form?
How to detect change in HTML page after a successful redirect from form?

Time:12-26

views.py

class ChangePasswordView(PasswordChangeView):
    form_class = ChangePasswordForm
    success_url = reverse_lazy("login")

login.html

 {% if XXXXXX %}
     <p style="color:green;">Password changed successfully! Please login.</p>
 {% endif %}

Basically I want the following message to appear on my login html page if password was changed. Is there a way to detect if form was successful (by passing some parameter to HTML), or if user was redirected from certain URL to current html page?

CodePudding user response:

Instead of writing a specific message, it might be easier to work with Django's message framework.

You can use the MessageSuccessMixin to add a message to the session, so:

from django.contrib.messages.views import SuccessMessageMixin


class ChangePasswordView(SuccessMessageMixin, PasswordChangeView):
    form_class = ChangePasswordForm
    success_url = reverse_lazy('login')
    success_message = 'Password changed successfully! Please login.'

Usually on all pages you then write logic to report messages to the user, as specified in the documentation:

{% if messages %}
<ul >
    {% for message in messages %}
    <li{% if message.tags %} {% endif %}>{{ message }}</li>
    {% endfor %}
</ul>
{% endif %}

In case there are messages, that page will deliver these to the user, and remove these from the session to prevent showing these a second time. It is thus a way to add messages that will (later) be reported to the user when they visit the next page.

  • Related