Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I style Django validation errors with Bootstrap?

When a form in Django has validation errors, the errors are given in a list with class errorlist.

Elements can be given an error style with Bootstrap by setting class="alert alert-error".

What is the best way to combine these, and use Bootstrap's error style for validation errors on Django forms?

like image 966
Gelatin Avatar asked Jul 04 '12 02:07

Gelatin


2 Answers

In Twitter Bootstrap, input elements are enclosed between "control-group" div or fieldset. So I would do something like this in template

{%for field in form %}
<div class="control-group {%if field.errors %}error{%endif%}">
{# render your field #}
</div>
{% endfor %}

Note: In bootstrap, class="alert alert-error" seems to be for alert messages and not for field specific errors.

like image 52
Rohan Avatar answered Sep 28 '22 02:09

Rohan


In Bootstrap 3, input elements are enclosed between "form-group" divs and the error class has changed to has-error.

{%for field in form %}
  <div class="form-group {%if field.errors %}has-error{%endif%}">
    {# render your field #}
  </div>
{% endfor %}
like image 35
AdamG Avatar answered Sep 28 '22 02:09

AdamG