title: Django: How to check if there are field errors from within custom widget definition?
body: <p>I'd like to create widgets that add specific classes to element markup when the associated field has errors.</p>
  
  <p>I'm having a hard time finding information on how to check whether a field has errors associated with it, from within widget definition code.</p>
  
  <p>At the moment I have the following stub widget code (the final widget will use more complex markup).</p>
  
  <pre><code>from django import forms
  from django.utils.safestring import mark_safe
  class CustomTextWidget(forms.Widget):
          def render(self, name, value, attrs):
              field_has_errors=False # change to dynamically reflect field errors, somehow
              if field_has_errors:
                  error_class_string="error"
              else:
                  error_class_string="" 
              return mark_safe(
              "&lt;input type=\"text\" class=\"%s\" value=\"%s\" id=\"id_%s\" name=\"%s\"&gt;" % (error_class_string, value, name, name)
              )
  </code></pre>
  
  <p>Can anyone shed light on a sensible way to populate the field_has_errors Boolean here? (or perhaps suggest a better way to accomplish what I'm trying to do). Thanks in advance.</p>