Opened 18 years ago
Closed 18 years ago
#4932 closed (duplicate)
class and id attributes on labels and input html tag
| Reported by: | Owned by: | nobody | |
|---|---|---|---|
| Component: | Forms | Version: | dev |
| 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 |
Description
Hi,
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.
or
for simple requirement
we could just pass the f.as_p as context to the template
Change History (4)
comment:1 by , 18 years ago
comment:2 by , 18 years ago
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 by , 18 years ago
| Triage Stage: | Unreviewed → Design decision needed |
|---|---|
| Version: | 0.96 → SVN |
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 by , 18 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
Closing as a dupe of #3515, which is becoming the "master" ticket for improved customizations of this sort.
Hi,
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 %}
thanks
james