Yeah, no problem. This is a snippet I've used before. If there aren't
errors it just wraps a div around the label and input element. If
there are errors it appends a class of 'error' to the div.

{% for field in form %}
  {% if field.errors %}
    <div class="error">
  {% else %}
    <div>
  {% endif %}
  {% if field.field.required %}<span class="required">*</span>{% endif
%}
    {{ field.label_tag }}{% if field.errors %} <span
class="error">&#8212; {{ field.errors|first|lower|striptags }}</
span>{% endif %}
    {{ field }}
    </div>
{% endfor %}

CSS just looks like:

.error label { ... }
.error input { ... }

On Feb 24, 8:34 pm, kkerbel <kker...@gmail.com> wrote:
> @jbergantine
>
> I'm curious about this too...could you elaborate?
>
> On Feb 24, 5:44 pm, jbergantine <jbergant...@gmail.com> wrote:
>
>
>
> > One option is to use the field.errors logic to decide whether to wrap
> > an error class around the input and then use descendent selectors to
> > target the input element with CSS. (.error input { ... }).
>
> > On Feb 24, 4:10 pm, Pugglewuggle <pugglewug...@gmail.com> wrote:
>
> > > Hi,
>
> > > I'm making a form and would like to use CSS classes to give fields
> > > different styles based on validation events, i.e. error or not. I'm
> > > using the following code as of now.
>
> > >         {% for field in form %}
> > >                 <tr>
> > >                 {% if field.errors  %}
> > >                         <th>{{ field.label_tag }}</th>
> > >                 {% else %}
> > >                         <th>{{ field.label_tag }}</th>
> > >                 {% endif %}
> > >                         <td>{{ field }}</td>
> > >                         <td>{{ field.errors|striptags }}</td>
> > >                 </tr>
> > >         {% endfor %}
>
> > > Is there any way to give the <input> element that's created a class
> > > tag? or other tags? Is there any way to prepopulate the value the
> > > rendered element using the template language? I'm essentially trying
> > > to create "pretty" and user friendly validation errors.
>
> > > Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to