Click here to Skip to main content
15,905,420 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am creating a registration form using the Django framework and I want to display some error messages to the user if they enter the wrong confirm password, or an email already taken etc. I have written the code and it seems to be working, but I can't seem to get the messages to show on screen upon redirecting to back to registration page if there is an error in the form. I have imported messages on the page (from django.contrib import messages) and I think my is all configured correct:[^]

Here is my code for

def register(request):
if request.method == "GET":
    register_form = RegisterForm()
    return render(request, "main/register.html", {
        'form': register_form
    register_form = RegisterForm(request.POST)
    if register_form.is_valid():
        first_name = request.POST['first_name']
        last_name = request.POST['last_name']
        username = request.POST['username']
        email = request.POST['email']
        password = request.POST['password']
        confirm_password = request.POST['confirm_password']

        if password == confirm_password:
            if User.objects.filter(email=email).exists():
          , 'Email or user name Already taking')
                    return redirect('register')
            elif User.objects.filter(username=username).exists():
          , 'username is taken')
                    return redirect('register')
                first_name=first_name, last_name=last_name, email=email, 
                return redirect('main/login.html')
            messages.error(request, 'Password Not Match')
            return redirect('register')   
        #return redirect ('/')     
        return render(request, 'main/login.html')

and my code for the register form:

<form action="{% url 'register' %}" method="POST">
               {% csrf_token %}
                   <legend>Enter details</legend>
                       {{ form.as_table }}

                       <button type="submit" class="mybutton _f-purple" value="submit">Register</button>

Thanks in advance to anyone that can help :)

What I have tried:

I have checked all settings are correct and have tried my webpage to view it's behaviour when form information is invalid

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900