Opened 12 years ago

Closed 12 years ago

#4932 closed (duplicate)

class and id attributes on labels and input html tag

Reported by: James <james_027@…> Owned by: nobody
Component: Forms Version: master
Severity: Keywords: newforms
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no



I am just learning the django's newform, I am also very new to django. I don't know about the old form, but I found this newform to be very cool! I hope it could provide a way for me to define the class and id attribute of the labels and input tag. So that I would just do something like this so make development stupidly fast...

f = MyForm()
f.as_p() # With this I'll just copy and paste it to my html file.


for simple requirement

we could just pass the f.as_p as context to the template

Change History (4)

comment:1 Changed 12 years ago by James <james_027@…>


In addition to my feature request, it could be nice if we have option for the as_p(), as_li(), as_table() method to return html code which will produce something like this ...

{% if field.errors %}<dd class="myerrors">{{ field.errors }}</dd>{% endif %}


comment:2 Changed 12 years ago by James Bennett

The as_p, as_li and as_table` shortcuts are intended for situations where you are happy with default markup for the form; if you require customization of the form's presentation, that's easy enough to do in the template.

comment:3 Changed 12 years ago by flother

Triage Stage: UnreviewedDesign decision needed
Version: 0.96SVN

You can choose the attributes to have on a widget (i.e. HTML element):

>>> import django.newforms as forms
>>> f = forms.CharField(widget=forms.TextInput(attrs={'class': 'blah'}))
>>> f.widget.render('name', 'value')
u'<input type="text" class="blah" value="value" name="name" />'

As for attributes on the label element: these are handled by BoundField.label_tag, as called in BaseForm._html_output (at least for as_p, as_table, and as_ul). You could sub-class this to handle custom attributes, but it might be worth adding an easier way into django.newforms proper.

comment:4 Changed 12 years ago by James Bennett

Resolution: duplicate
Status: newclosed

Closing as a dupe of #3515, which is becoming the "master" ticket for improved customizations of this sort.

Note: See TracTickets for help on using tickets.
Back to Top