Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#18733 closed New feature (wontfix)

Form ids on as_table

Reported by: tturner Owned by: nobody
Component: Forms Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When you render a form as a table in a template it does the following.

<tr><th><label for="id_component_a">Component A:</label></th><td><input type="checkbox" name="component_a" id="id_component_a"></td></tr>

The problem is if I want to change the CSS or access certain parts in JavaScript I can't. So what I have done is added an id to each part.

<tr id="_tr_id_component_a"><th id="_th_id_component_a"><label for="id_component_a">Component A:</label></th><td id="_td_id_component_a"><input type="checkbox" name="component_a" id="id_component_a"></td></tr>

so now the tr, th, td tag has an id.

The code I had to change was.

def as_table(self):
        "Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
        return self._html_output(
            normal_row = u'<tr%(html_class_attr)s id="_tr_%(id)s"><th id="_th_%(id)s">%(label)s</th><td id="_td_%(id)s">%(errors)s%(field)s%(help_text)s</td></tr>',
            error_row = u'<tr><td colspan="2">%s</td></tr>',
            row_ender = u'</td></tr>',
            help_text_html = u'<br /><span class="helptext">%s</span>',
            errors_on_separate_row = False)

I also had to pass the id to the routine

If you want a patch I can create one

Change History (5)

comment:1 Changed 3 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

You can traverse the DOM in javascript and locate items that do not have ids relative to ones that do (or within containers external to this rendering that have classes or ids specified). So I think the statement that you cannot access certain parts of this rendering in javascript is not true, therefore I don't see the need to add all these ids in the rendering.

comment:2 Changed 3 years ago by tturner

  • Resolution wontfix deleted
  • Status changed from closed to reopened

How messy django is here to help you develop and speed you up!!!

It not just JavaScript that you need access.
I have needed to change the CSS for some of the labels. For example I needed to change the font size.

I am sure that I not the only one asking

If you think you can do this without ids please show me an example.

comment:3 Changed 3 years ago by kmtracey

  • Resolution set to wontfix
  • Status changed from reopened to closed

I'm not sure what you are asking to be shown how to do so I'm at a loss to give you an example of how to do it. I suggest you ask a specific question for what you'd like to do on someplace like django-users or #django IRC. Neither CSS selectors nor javascript require that each and every element in the DOM have an id attached to it in order to apply styles or manipulate the elements with javascript. (And I rather suspect the suggested change creates invalid HTML by duplicating the same id values on multiple elements, though I have not confirmed that.)

Please do not respond here with more specifics of what it is you'd like an example of. Trac is not the place for user support, which is what this is veering towards. There are many more productive places to ask for help with how to do things in Django. I'm very sure whatever it is you are trying to do is possible without adding ids to all the HTML elements generated by forms when output as tables.

comment:4 Changed 3 years ago by tturner

I am replying and I am sorry this was not user support it was a real case enhancement request

If you have a form defined by

{{ form }}

which then generates a table

<tr><th><label for="id_name">Name:</label></th><td><input type="text" name="name" id="id_name"></td></tr>
<tr><th><label for="id_description">Description:</label></th><td><input type="text" name="description" id="id_description"></td></tr>

you can not change the CSS of the name like the font size

Last edited 3 years ago by tturner (previous) (diff)

comment:5 Changed 3 years ago by kmtracey

Certainly you can write a css rule that will apply to whatever aspect of the table you'd like to affect. If you ask how on django-users or IRC likely someone will help.

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