Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 class="messages">
    {% for message in messages %}
    <li{% if message.tags %} class="{{ 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading