html - django forms: styling widget when field is required or has an error -


i'm creating django form , can't seem access each field's widget style differently when there's error or when field required (eg: highlight field's box).

html:

{% block content %}   <form action="" method="post" class="form">  {% csrf_token %}     {% field in form %}       <div class="fields-container">         <p class="label">{{ field.label }}</p>           {{ field }}       </div>       {{ field.errors }}      {% endfor %}     <button type="submit" id="form-button">submit</button>   </form> {% endblock %} 

css:

.label {   margin: 0 5px 0 0;   width: 20%;   text-align: left; }   #id_full_name, #id_email, #id_message {   margin: 0;   flex: 1 1; }  #id_message {              resize: none;   overflow: auto; }   .errorlist {                /*default css class form errors*/   color: 999999;   font-size: 75%;   font-weight: bold;   list-style: none;   margin-bottom: 5px;   align-self: flex-start;        } 

i've been accessing , styling widgets using #id_fieldname, i've tried following , didn't work:

#id_fieldname.required, id_fieldname.error { border: 2px solid red; } 

thanks in advance!

you add class 'required' in container <div>:

<div class="fields-container {% if field.field.required %}required{% endif %}"> 

using django form methods form.as_p, form.as_ul, form.as_table, done automatically if declare specific css class required fields in form, example:

class myform(forms.form):      required_css_class = 'required'      ... 

more in documentation


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -