Home > front end >  Django's UserCreationForm's Errors isn't working?
Django's UserCreationForm's Errors isn't working?

Time:05-27

few months ago i made a website where it worked but similar code this time isn't working! it returns : ValueError at /registration/ The User could not be created because the data didn't validate. this is my form.py in bottom:

class CreateUserForm(UserCreationForm):
    class Meta:
        model = User
        fields = ["username", "email", "password1", "password2"]

my views:

from .form import CreateUserForm

def registrationPage(request):
    form = CreateUserForm()
    if request.method == "POST":
        form = CreateUserForm(request.POST)
        if form.is_valid:
            form.save()
            return redirect("login")
        else:
            form = CreateUserForm()

    context = {"form": form}
    return render(request, "store/registration.html", context)

in HTML previously i used :

{{form.errors}}

& it used to show me work on the page but not anymore

CodePudding user response:

When this error you get then do this:

  1. You must type strong password, which is provided by Django.

password validation rules:

At least 1 digit;
At least 1 uppercase character;
At least 1 lowercase character;
At least 1 special character;

for example:

 Example@4089
  1. This error will come while confirming your password, you must enter correct password in both the fields.

  2. you will face this error if user is already exist.

  3. you must check exists user while creating user, if user exist, this error will come.

CodePudding user response:

According to your comment above ("i want django to show the error messages"[if password is weak in example]), you should add this to your html page (instead than {{form.errors}}).

<div >
     {% if form.non_field_errors %} 
                        
        {% for error in form.non_field_errors %}
           <p style="font-size: 13px;">
              {{ error|escape }}
           </p>
       {% endfor %} 
                        
     {% endif %}
 </div>

Form.non_field_errors() This method returns the list of errors from Form.errors that aren’t associated with a particular field. This includes ValidationErrors that are raised in Form.clean() and errors added using Form.add_error(None, "...").

  • Related