How should I erase Django validation errors using Bootstrap?

If the Django form has validation errors, the errors are listed with the errorlist class.

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

What is the best way to combine them and use the Bootstrap error style for validation errors on Django forms?

+6
source share
3 answers

On Twitter Bootstrap, input elements are enclosed between a "control-group" div or fieldset . So I would do something like this in the 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" appears for warning messages, not specific errors.

+7
source

In Bootstrap 3, input elements are enclosed between the "form-group" div 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 %} 
+4
source

It has already been written! :)

These libraries act as a wrapper around your form and add the appropriate class names that are used in Django Bootstrap, so you have a basic style (default for bootstrap)

+1
source

Source: https://habr.com/ru/post/919641/


All Articles