title: django - how can I access the form field from inside a custom widget
body: <p>The below class inherits from the <strong>Textarea</strong> widget and features javascript code that displays how many characters more a user can enter in a textarea. </p>
  
  <pre><code>class TextAreaWithCharCounter(forms.Textarea):
  
      class Media:
          js = ('js/jquery.charcounter.js',)
  
      def render(self, name, value, attrs = None):
          id = attrs['id']
          max_length = self.attrs.get('max_length', 200)
          output = super(TextAreaWithCharCounter, self).render(name, value, attrs)
          output += mark_safe(u'''
                          &lt;script type="text/javascript"&gt;
                          $("#%s").charCounter(%d, {classname:"charcounter"});
                          &lt;/script&gt;'''%(id, max_length))        
          return output
  </code></pre>
  
  <p>The relevant portion of the form code is as follows:</p>
  
  <pre><code>class MyForm(forms.Form):
      foo = forms.CharField(max_length = 200, widget = TextAreaWithCharCounter(attrs={'max_length':200}))
      ...
  </code></pre>
  
  <p>You can see that I pass the <strong><code>max_length</code></strong> argument twice, one for the field and one for the widget. A better way may be accessing the form field from inside the widget and get its max_length attribute, so that a max_length argument won't be required by the widget. How can I do that?</p>