Honza Král wrote:
>> Is it possible to have css classes attached to newforms widgets? I would
>> like to be able to distinguish between fx. checkboxes, radio buttons,
>> submit buttons, text fields (which are all <input />'s).

> well, you can pass args={'class' : 'whatever'} to the widget's __init__()
> or you could subclass the widget and do that inside

This does (IMHO) not comply very much with the DRY philosophy of django.

> but there is currently no class set globally for all widgets
> 
>> In the oldforms module when having a required charfield, there would be
>> a class="vTextField required" to make CSS styling easier.
>>
>> Am I overlooking something, or is newforms missing this?

I was able to hack newforms by adding this in the __init__ of Field:

  # Attach field + widget type css classes
  self.css_class = type(self).__name__ + " " + type(widget).__name__

  # Add an extra class, Required, if applicable
  if self.required:
    self.css_class += " Required"

Furthermore, to handle the output, I edited the _html_output of BaseForm
and added an extra item in the formatting dict: 'class': field.css_class

At last I updated the output functions (as_table, as_ul and as_p) to
have a 'class="%(class)s"'.

All this results in the following HTML structure for a required
CharField with a (default) TextInput widget:

  <p class="CharField TextInput Required">
   <label for="id_charfield">
    CharField:
   </label>
   <input type="text" name="charfield" id="id_charfield" />
  </p>

Just thought I would mention my solution if anyone else is interested ;)

Also, please let me know if anyone else have an easier solution to this.

-- 
Christian Joergensen | Linux, programming or web consultancy
http://www.razor.dk  |     Visit us at: http://www.gmta.info

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to