Opened 4 years ago
Closed 4 years ago
#32354 closed Bug (invalid)
HTML in verbose_name
Reported by: | Serl | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 3.1 |
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
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:
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!
Change History (2)
comment:2 by , 4 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
verbose_name
should be "a human-readable name for the field", see docs. You can use 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 support channels for support questions.
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. (https://docs.djangoproject.com/en/3.1/topics/db/models/) So I think "move the
capfirst
directly toset_attributes_from_name
" is not probably proper. The better way I have just thought is to keep original type: passSaveString
and returnSafeString
.