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.
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>
* 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>