Code

Opened 7 years ago

Closed 7 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: UI/UX:

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

Attachments (0)

Change History (4)

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

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

comment:2 Changed 7 years ago by ubernostrum

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 7 years ago by flother

  • Triage Stage changed from Unreviewed to Design decision needed
  • Version changed from 0.96 to 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 Changed 7 years ago by ubernostrum

  • Resolution set to duplicate
  • Status changed from new to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.