Search code examples
djangopython-3.xdjango-formsdjango-registration

Django Registration Redux - How to get error messages to display at correct fields.


I am using Django Registration Redux and wanted to get some custom field styling done. However my error messages are not showing up as intended in template. I have tried to get it to show using {{form.errors}} and several variations on this without success.

I would like to have the correct error message show up at the right field as it would when you use Registration Redux out of the box.

                <form action="." method="POST">
                    {% csrf_token %}
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group no-margin">
                                {{form.username | add_some_css:"form-control" }}
                            </div>
                            <span class="help-block">Your profile - http://snapper.com/<strong>username</strong></span>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group margin-top-30">
                                {{form.email | add_some_css:"form-control" }}
                            </div>
                            <div class="form-group">
                                {{form.password1 | add_some_css:"form-control" }}
                            </div>
                            <div class="form-group">
                                {{form.password2 | add_some_css:"form-control" }}
                            </div>

                            <div class="form-group no-border">
                                <button type="submit" class="btn btn-danger btn-block">Sign up</button>
                            </div>
                        </div>
                    </div>
                </form>

Solution

  • You can show the errors in following way for form non-field types as well as specific field type errors. Consider the example of password_change form that I have used:-

    <form method="POST" action="{% url 'account_change_password' %}" class="password_change signup">
                {{form.non_field_errors}}
                {% csrf_token %}
                <p><label for="id_oldpassword">Current Password:</label> <input id="id_oldpassword" name="oldpassword" placeholder="Current Password" type="password" required class="sigup_input"/></p>
                <p>{{form.oldpassword.errors}}</p>
                <p><label for="id_password1">New Password:</label> <input id="id_password1" name="password1" placeholder="New Password" type="password" required class="sigup_input"/></p>
                <p>{{form.password1.errors}}</p>
                <p><label for="id_password2">New Password (again):</label> <input id="id_password2" name="password2" placeholder="New Password (again)" type="password" required class="sigup_input"/></p>
                <p>{{form.password2.errors}}</p>
                <button type="submit" name="action">{% trans "Change Password" %}</button>
            </form>
    

    Notice the use of {{form.field_name.errors}}

    I hope you got your answer.