Change Password - How to Use Django Messages Framework
Django project already comes with the messages framework installed. In this article I will show How to Use Django Messages Framework
INSTALLED_APPS
django.contrib.messages
MIDDLEWARE or MIDDLEWARE_CLASSES in older versions:
django.contrib.sessions.middleware.SessionMiddleware
django.contrib.messages.middleware.MessageMiddleware
TEMPLATES
context_processors
django.contrib.messages.context_processors.messages
Settings.py
1 2 3 4 5 6 7 8 9 10 | #settings.py from django.contrib.messages import constants as messages MESSAGE_TAGS = { messages.DEBUG: 'alert-info' , messages.INFO: 'alert-info' , messages.SUCCESS: 'alert-success' , messages.WARNING: 'alert-warning' , messages.ERROR: 'alert-danger' , } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #views.py from django.shortcuts import render, redirect from django.contrib.auth.decorators import login_required from django.contrib import messages from django.contrib.auth import update_session_auth_hash from django.contrib.auth.forms import PasswordChangeForm @login_required def password(request): if request.method = = 'POST' : form = PasswordChangeForm(request.user, request.POST) if form.is_valid(): user = form.save() update_session_auth_hash(request, user) # Important! messages.success(request, 'Your password was successfully updated!' ) return redirect( '/password' ) else : messages.error(request, 'Please correct the error below.' ) else : form = PasswordChangeForm(request.user) return render(request, 'change_password.html' , { 'form' : form }) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | //templates/change_password.html <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "utf-8" > <meta name= "viewport" content= "width=device-width, initial-scale=1, shrink-to-fit=no" > <title>{% block title %} Change Password {% endblock title %}</title> {% load static %} </head> <body> <div style= "padding:25px;" > <p><h1>Change Password - How to Use Django Messages Framework</h1></p> {% if messages %} <ul class = "messages" > {% for message in messages %} <li class = "{{ message.tags }}" >{{ message }}</li> {% endfor %} </ul> {% endif %} <form method= "post" > {% csrf_token %} {{ form }} <button type= "submit" >Save changes</button> </form> </div> <style> ul {list-style:none;} .alert-success, .alert-warning, .alert-info, .alert-danger { background-repeat: no-repeat; background-position: 10px center; height: 20px; text-transform: uppercase; font-size: 11px; line-height: 22px; margin-bottom: 20px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 50px; } .alert-success { background-color: #EBF8D6; border: 1px solid #A6DD88; color: #539B2D; background-image: url({% static 'img/accept00.png' %}); } .alert-warning { background-color: #FFECE6; border: 1px solid #FF936F; color: #842100; background-image: url({% static 'img/delete00.png' %}); } .alert-info { background-color: #D3EEF1; border: 1px solid #81CDD8; color: #369CAB; background-image: url({% static 'img/info0000.png' %}); } .alert-danger { background-color: #FFFBCC; border: 1px solid #FFF35E; color: #C69E00; background-image: url({% static 'img/warning0.png' %}); } .close-notification { width: 16px; height: 16px; position: absolute; background: url({% static 'img/close000.png' %}) no-repeat; top: 4px; right: 4px; cursor: pointer; } </style> </body> </html> |
1 2 3 4 | //If the error message was added, the output would be something like that: <ul class = "messages" > <li class = "alert-danger" >Please correct the error below.</li> </ul> |
1 2 3 4 5 6 7 8 9 10 | #urls.py from django.contrib import admin from django.urls import path from myapp import views from django.conf.urls import url urlpatterns = [ path( 'admin/' , admin.site.urls), path( 'password' ,views.password), ] |