[Django] #32354: HTML in verbose_name

12 views
Skip to first unread message

Django

unread,
Jan 14, 2021, 10:07:03 AM1/14/21
to django-...@googlegroups.com
#32354: HTML in verbose_name
--------------------------------------+------------------------
Reporter: serl | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 3.1
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 |
--------------------------------------+------------------------
Hello,

I'm having some issues with the `verbose_name` of some of the fields in my
models.

In particular, I'd like to put some HTML in there. Here's an example:

{{{#!python
short_ohohoh = models.CharField(
mark_safe("<abbr title='This is way longer'>Short</abbr> ohohoh"),
max_length=100,
)
}}}

The problem is: even if I `mark_safe`, the verbose_name is escaped when I
try to get it in my templates with: `{{ form.short_ohohoh.label }}` or `{{
form.short_ohohoh.label_tag }}`.

----

I dug in Django's code, and I found out that the verbose_name passes
through a `capfirst` here in `formfield`:
https://github.com/django/django/blob/75182a800a621b7a5b2c0a1f39a56e753b9a58ca/django/db/models/fields/__init__.py#L916.

That way, my SafeString is transformed back to str, I guess. I tried to
remove the `capfirst` and assigning directly `verbose_name` and my HTML is
preserved (but fields without `verbose_name` are, obviously, lowercase).

As a workaround, in my templates, I can `mark_safe` it again, but sadly
the Django admin displays my fields incorrectly.

----

So yeah, for starters: is there a better way to deal with acronymns in my
models?
Then, I would move the `capfirst` directly to `set_attributes_from_name`
in
https://github.com/django/django/blob/75182a800a621b7a5b2c0a1f39a56e753b9a58ca/django/db/models/fields/__init__.py#L772.
Is there a reason why it is not already there?

Thanks!

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

Django

unread,
Jan 14, 2021, 11:57:49 PM1/14/21
to django-...@googlegroups.com
#32354: HTML in verbose_name
-----------------------------+--------------------------------------
Reporter: Sergio Livi | Owner: nobody

Type: Bug | Status: new
Component: Forms | Version: 3.1
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 Guan):

The document notes that: **The convention is not to capitalize the first
letter of the verbose_name. Django will automatically capitalize the first
letter where it needs to.** So I think "move the {{{capfirst}}} directly
to {{{set_attributes_from_name}}}" is not probably proper. The better way
I have just thought is to keep original type: pass {{{SaveString}}} and
return {{{SafeString}}}.

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

Django

unread,
Jan 15, 2021, 12:51:56 AM1/15/21
to django-...@googlegroups.com
#32354: HTML in verbose_name
-----------------------------+--------------------------------------
Reporter: Sergio Livi | Owner: nobody
Type: Bug | Status: closed
Component: Forms | Version: 3.1
Severity: Normal | Resolution: invalid

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 Mariusz Felisiak):

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


Comment:

`verbose_name` should be ''"a human-readable name for the field"'', see
[https://docs.djangoproject.com/en/3.1/ref/models/fields/#verbose-name
docs]. You can use
[https://docs.djangoproject.com/en/3.1/ref/forms/api/#django.forms.BoundField.label
label] on a form field for using HTML in labels.

> So yeah, for starters: is there a better way to deal with acronymns in
my models?

Please use one of
[https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels
support channels] for support questions.

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

Reply all
Reply to author
Forward
0 new messages