[Django] #29153: add label attrs to BaseForm

7 views
Skip to first unread message

Django

unread,
Feb 23, 2018, 10:47:16 AM2/23/18
to django-...@googlegroups.com
#29153: add label attrs to BaseForm
----------------------------------------+------------------------
Reporter: ChristophRob | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: 2.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
----------------------------------------+------------------------
As the label_tag is already set up to give the label class attributes and
label_suffix, it is quite easy to add this to the BaseForm _html_output()

{{{
class BoundField:
def label_tag(self, contents=None, attrs=None, label_suffix=None):
}}}


{{{
class BaseForm:
def _html_output(self, normal_row, error_row, row_ender,
help_text_html, errors_on_separate_row):
# ...
label = bf.label_tag(label) or ''
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29153>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 23, 2018, 11:02:37 AM2/23/18
to django-...@googlegroups.com
#29153: add label attrs to BaseForm
------------------------------+--------------------------------------

Reporter: ChristophRob | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: 2.0
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------

Comment (by Tim Graham):

I don't understand what change you're proposing. Can you provide a patch
or describe the changes and the use case?

--
Ticket URL: <https://code.djangoproject.com/ticket/29153#comment:1>

Django

unread,
Feb 23, 2018, 11:51:41 AM2/23/18
to django-...@googlegroups.com
#29153: add label attrs to BaseForm
------------------------------+--------------------------------------

Reporter: ChristophRob | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: 2.0
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------
Description changed by ChristophRob:

Old description:

> As the label_tag is already set up to give the label class attributes and
> label_suffix, it is quite easy to add this to the BaseForm _html_output()
>
> {{{
> class BoundField:
> def label_tag(self, contents=None, attrs=None, label_suffix=None):
> }}}
>

> {{{
> class BaseForm:
> def _html_output(self, normal_row, error_row, row_ender,
> help_text_html, errors_on_separate_row):
> # ...
> label = bf.label_tag(label) or ''
> }}}

New description:

Goal:
Add attributes like "class" to the label tag for inputs.

Problem:
Actually the only thing thats stops me from using {{ form }} is the
incapability to change classes for labels easily. So i have to do it like
this, for every form and every field:

{{{
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.subject.errors }}
<label for="{{ form.subject.id_for_label }}" class="MY-CUSTOM-
CLASS">Email subject:</label>
{{ form.subject }}
</div>

}}}

Where it should be changed:
I would be fine if the label_attrs could be set in BaseForm(), but i think
the right place would be class Field

As the label_tag is already set up to give the label class attributes and
label_suffix, it is quite easy to add this to the BaseForm _html_output()

{{{
class BoundField:
def label_tag(self, contents=None, attrs=None, label_suffix=None):
}}}


{{{
class BaseForm:
def _html_output(self, normal_row, error_row, row_ender,
help_text_html, errors_on_separate_row):
# ...
label = bf.label_tag(label) or ''
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/29153#comment:2>

Django

unread,
Feb 23, 2018, 11:14:45 PM2/23/18
to django-...@googlegroups.com
#29153: Ease customizing the label attrs on a model form field
------------------------------+--------------------------------------
Reporter: ChristophRob | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: 2.0
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => wontfix


Comment:

Thanks for the clarifying edits. I guess you're running into the problem
described on [https://stackoverflow.com/questions/6959178/how-to-set-css-
class-of-a-label-in-a-django-form-declaration Stack Overflow].

I'm not sure if the use case is common enough to add a field attribute for
this. Usually I style labels using CSS3 selectors. If you can get
consensus on a proposal on the DevelopersMailingList, we can reopen the
ticket. Generally, the consensus has been that if you need to make much
customization to form rendering, you should iterate over the fields rather
than using `{{ form }}`.

--
Ticket URL: <https://code.djangoproject.com/ticket/29153#comment:3>

Django

unread,
Feb 26, 2018, 5:06:20 AM2/26/18
to django-...@googlegroups.com
#29153: Ease customizing the label attrs on a model form field
------------------------------+--------------------------------------
Reporter: ChristophRob | Owner: nobody
Type: New feature | Status: closed
Component: Forms | Version: 2.0
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------+--------------------------------------

Comment (by ChristophRob):

Replying to [comment:3 Tim Graham]:


> Thanks for the clarifying edits. I guess you're running into the problem
described on [https://stackoverflow.com/questions/6959178/how-to-set-css-
class-of-a-label-in-a-django-form-declaration Stack Overflow].
>
> I'm not sure if the use case is common enough to add a field attribute
for this. Usually I style labels using CSS3 selectors. If you can get
consensus on a proposal on the DevelopersMailingList, we can reopen the
ticket. Generally, the consensus has been that if you need to make much
customization to form rendering, you should iterate over the fields rather
than using `{{ form }}`.

Thank you Tim, for your response. Yes indeed, thats exactly my problem. As
i am using bootstrap 3 it is necessary, to set a "control-label" class to
the label. Especially for errors in form validation. It seems to me that
in bootstrap 4, the team just gave up, and does not design labels with
form errors anymore by default. So i am also gonna change to bootstrap 4,
and sadly no label error design. So i think you are right, to not fix
this.

Still it makes me wonder that the customizable label_tag is already half
implemented.

--
Ticket URL: <https://code.djangoproject.com/ticket/29153#comment:4>

Reply all
Reply to author
Forward
0 new messages