UserCreationForm Errors Not Showing in HTML

Hello Nice Guys,
i have a custom account model and model form for registration, i check that form errors is already attached to my form on views.
but on html when i access it with this it does not appear any errors in html.

<form class="ui large form" method="POST" action="">
    {% csrf_token %}
    {% for field in registration_form %}
    <p>{{ field.label_tag }}</p>
    <p>{{ field }}</p>
    {% for error in registration_form.field.errors %}
    <div class="ui error message">
        {{ error }}
    </div>
    {% endfor %}
    {% endfor %}
    <button class="ui button" type="submit">Register</button>
</form>

on Views context[‘registration_form’] is already have the errors.
Views:


def account_register(request):
    page = 'Account Register'
    context = {
        'page': page,
    }
    if request.POST:  # POST Request
        form = RegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            email = form.cleaned_data.get('email')
            raw_password = form.cleaned_data.get('password1')
            account = authenticate(email=email, password=raw_password)
            login(request, account)
            return redirect('pages:index')
        else:
            context['registration_form'] = form
    else:  # GET Request
        form = RegistrationForm()
        context['registration_form'] = form
    return render(request, 'accounts/account_register.html', context)

Forms:

class RegistrationForm(UserCreationForm):
    email = forms.EmailField(max_length=255, help_text='Required, Please Add Valid E-Mail Address.', required=True)

    class Meta:
        model = Account
        fields = ('email', 'username', 'password1', 'password2')

thanks in advance.

That’s not the right reference for the errors.

At that point in the loop field is a reference to the field in the form, so your reference to the errors should be field.errors, not registration_form.field.errors.

You may also want to render registration_form.non_field_errors.

See the docs at Working with forms | Django documentation | Django for further information.

1 Like

Thanks Buddy Solved.