0

I'm trying to display an error message for a certain error that user makes while registering, for now I've only covered an error regarding password and conf_passowrd not matching, but only that scenario works, whenever there is an error regarding, for example, too short password entered, it still displays the error regarding password mismatch.

What I've tried so far:

view.py:

from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.forms import UserCreationForm


def indexsignup(request):
    form = UserCreationForm()
    if request.method == 'POST':
        regForm = UserCreationForm(request.POST)
        if regForm.is_valid():
            regForm.save()
            return redirect('login')
        else:
            for msg in form.error_messages:
                if len('password1') < 6:
                    messages.error(request, f"PAss short")
                    print(msg)
                else:
                    messages.error(request, f"Two passwords don't match.")
                    print(msg)     
    return render(request, 'registration/register.html', {'form':form})

register.html:

{% load static %}

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Forum - SignUp</title>
    <link rel="stylesheet" href="{% static 'signup/style.css' %}">
</head>

<body>
    <div class="signin-box">
        <h1> Sign Up</h1>
        <form method="post" action="">
            {% csrf_token %}
            <div class="textbox">
                <input type="text" name="username" placeholder="Username" maxlength="10" autocapitalize="none"
                    autocomplete="username" autofocus="" required="" id="id_username">
            </div>
            <div class="textbox">
                <input type="password" name="password1" placeholder="Password" autocomplete="new-password" required=""
                    id="id_password1">
                <script type="text/javascript">
                    function reveal() {
                        if (document.getElementById('box').checked) { document.getElementById("id_password1").type = 'text'; }
                        else
                            document.getElementById("id_password1").type = 'password';
                    }
                </script>
                <div class="check">
                    <input title="Reveal password" type="checkbox" id="box" onclick="reveal()">
                </div>
            </div>
            <div class="textbox">
                <input type="password" name="password2" placeholder="Confirm Password" autocomplete="new-password"
                    required="" id="id_password2">
                    <script type="text/javascript">
                        function reveal2() {
                            if (document.getElementById('box').checked) { document.getElementById("id_password2").type = 'text'; }
                            else
                                document.getElementById("id_password2").type = 'password';
                        }
                    </script>
                    <div class="check">
                        <input title="Reveal password" type="checkbox" id="box" onclick="reveal2()">
                    </div>
            </div>
            {% if messages %}
            {% for message in messages %}
            <div class="help">
                <p><span style="font-size: 20px">&#9888;</span> {{message}}</p>
            </div>
            {% endfor %}
            {% endif %}
            <input class="btn" type="submit" style="opacity: 1 !important;" value="Sign Up">
        </form>
    </div>
</body>

1 Answers1

0

Best thing would be to add ValidationErrorin django form. You need to override clean method for password1 field (or any field where you want to add customization). Add the below function in UserCreationForm class.

def clean_password1(self):
    entered_password = self.cleaned_data['password1']
    if len(entered_password) <= 6:
        raise forms.ValidationError("Password should include more than 6 characters")
    return entered_password

You will be able to access these form errors in template using {{ form.errors }}. You can further check this on how to render form errors in template

Reference: https://docs.djangoproject.com/en/3.2/ref/forms/validation/#cleaning-a-specific-field-attribute

Amandeep Singh
  • 1,371
  • 1
  • 11
  • 33