Home > Net >  Django alert is not appearing
Django alert is not appearing

Time:07-08

I need help with my Django app :

I'm trying to use Django messages framework to display a message. And I don't know why my message is not showing up?

views.py :

 from django.contrib import messages

def login(request):

    return render(request, 'authorisation/login.html', {})


def register(request):
    if request.method == 'POST':
        email = request.POST['email'].replace('', '').lower()
        password1 = request.POST['password1']
        password2 = request.POST['password2']

        if not password1 == password2:
            messages.error(request, "Passwords do not match")
            return redirect('register')

        if User.objects.filter(email=email).exists():
            messages.error(
                request, "A user with the email address : {} already exists, please use a different email".format(email))
            return redirect('register')

my settings.py :

from pathlib import Path
import os
from django.contrib import messages


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

MESSAGE_TAGS = {
    messages.DEBUG: 'info',
    messages.INFO: 'info',
    messages.SUCCESS: 'success',
    messages.WARNING: 'warning',
    messages.ERROR: 'danger',
}

code in my template :

      {% for message in messages %}
      <div  role="alert">
        {{ messsage }}
      </div>
      {% endfor %} 
      {% block body %} {% endblock %}

when I tried to check the page source I found :

     <div  role="alert">
        
      </div>

CodePudding user response:

In Yor Template Double s

{{ message }}

Not Triple s

{{ messsage }}
  • Related