0

I am building a register form, but I am having some trouble with its validation.

I would like to see the error message showing up at the field, but instead I am getting a error on the browser saying :

The User could not be created because the data didn't validate.

Request Method:     POST
Request URL:    http://127.0.0.1:8000/account/register/
Django Version:     1.9.8
Exception Type:     ValueError
Exception Value:    

The User could not be created because the data didn't validate.

Exception Location:     C:\Python34\lib\site-packages\django\forms\models.py in save, line 446

This is my forms.py

class UserRegistrationForm(forms.ModelForm):
    password = forms.CharField(label='Password', required=False ,widget=forms.PasswordInput)
    password2 = forms.CharField(label='Repeat password', required=False  ,widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ('username', 'first_name', 'email')

    def clean_password2(self):
        password1 = self.cleaned_data.get('password1')
        password2 = self.cleaned_data.get('password2')
        #cd = self.cleaned_data
        if not password2:
            raise forms.ValidationError("Fill out the password2 .")
        if password1 != password2:
            raise forms.ValidationError("The two password fields didn't match.")
        return password2

This is my view register

def register(request):
    if request.method == 'POST':
        user_form = UserRegistrationForm(request.POST)
        if  user_form.is_valid:
            new_user = user_form.save(commit=False)
            new_user.set_password(user_form.cleaned_data['password'])
            new_user.save()
            return render(request, 'account/register_done.html', {'new_user': new_user})
        else:
            print (user_form.errors)
    else:
        user_form = UserRegistrationForm()
    return render(request, 'account/register.html', {'user_form': user_form})

my htmls - register.html

{% extends "account/base.html" %}
{% block title %}Create an account{% endblock %}
{% block content %}
    <h1>Create an account</h1>
    <p>Please, sign up using the following form:</p>
    <form action="." method="post">
        {{ user_form.as_p }}
        {% csrf_token %}
        <p><input type="submit" value="Create my account"></p>
    </form>
{% endblock %}

register_done.html

{% extends "account/base.html" %}
{% block title %}Welcome{% endblock %}
{% block content %}
    <h1>Welcome {{ new_user.first_name }}!</h1>
    <p>Your account has been successfully created. Now you can <a href="{% url "login" %}">log in</a>.</p>
{% endblock %}
4
  • I don't know why you are reinventing the wheel, you can use django-redux package Commented Oct 7, 2016 at 13:53
  • I am following a book, I am starting with django, so after finishing the book I will go for the packges. Commented Oct 7, 2016 at 13:55
  • That sounds nice, anyway which book are you following ? Commented Oct 7, 2016 at 14:16
  • The book is this one link. I strongly recommend it. I went through "Tango with Django" which is a good kick off. Commented Oct 7, 2016 at 19:27

1 Answer 1

2

Seems like you are not calling is_valid method, this may cause this issue:

if  user_form.is_valid 

Try to change above line to:

if user_form.is_valid()

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you buddy! I reallt missed it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.