Unable to see messages using django register

I have an app with views.py as follows

from django.shortcuts import render,redirect
from django.contrib.auth.models import User, auth
from django.contrib import messages
# Create your views here.
def register(request):

    if request.method == 'POST':
        first_name = request.POST['first_name']
        last_name = request.POST['last_name']

        username = request.POST['username']
        password1 = request.POST['password1']
        password2 = request.POST['password2']
        email = request.POST['email']

        if password1==password2:
            if User.objects.filter(username=username).exists():
                messages.info(request,'username taken')

                return redirect('register')
            elif User.objects.filter(email=email).exists():
                messages.info(request,'email taken')

                return redirect('register')
            else:
                user= User.objects.create_user(username=username, password=password1, email=email, first_name=first_name,last_name=last_name)
                user.save();

                print('user created')
        else:
            print('password missmatch')
            return redirect('register')
        return redirect('/')
    else:
        return render(request,'register.html')

I have added a for loop for showing the messages in register.html

    
{% for message in messages %}
      <h3> {{message}} </h3>
{% endfor %}

I can't able to see messages when the for loop is available but i can see message address when for loop is removed. Can some explain and help me how to proceed?

This might be your issue, reading the docs for the messages framework it says If you’re using the context processor, your template should be rendered with a RequestContext. Otherwise, ensure messages is available to the template context.

In your example, it would be something like this

from django.template import RequestContext
...
render(RequestContext(request), 'register.html')
Back to Top