#35716 closed Cleanup/optimization (fixed)
Undefined template variables on Admin's add page templates when using fieldsets
| Reported by: | Fábio Domingues | Owned by: | Sarah Boyce |
|---|---|---|---|
| Component: | contrib.admin | Version: | 5.1 |
| Severity: | Release blocker | Keywords: | |
| Cc: | Adam Johnson | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Applications that does not forgive undefined variables on templates, for example using this:
# Raise an exception when accessing an undefined variable in templates
class InvalidString(str):
def __mod__(self, other):
from django.template.base import TemplateSyntaxError
raise TemplateSyntaxError(f"Undefined variable or unknown value for: {other}")
raises an error "Undefined variable or unknown value for: fieldset.formset.prefix" on line 1 of django/contrib/admin/templates/admin/includes/fieldset.html
My sugestion is to use firstof tags instead of the with tag.
Change History (8)
comment:1 by , 14 months ago
| Cc: | added |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Type: | Bug → Cleanup/optimization |
comment:2 by , 14 months ago
| Needs tests: | set |
|---|---|
| Patch needs improvement: | set |
comment:3 by , 14 months ago
| Summary: | #35189 brokes admin/[model]/add pages when application does not allow undefined variables on templates → Undefined template variables on Admin's add page templates when using fieldsets |
|---|
comment:4 by , 14 months ago
| Needs tests: | unset |
|---|---|
| Owner: | set to |
| Patch needs improvement: | unset |
| Status: | new → assigned |
comment:5 by , 14 months ago
(Adam, I'm adding you as cc since you added yourself to #28526.)
Thanks :)
Honestly, I think the string_if_invalid feature brings more pain than it’s worth, after investigating it a bit for a client last year. You can see all the ways that I found it goes wrong in this blog post.
I’d still be happy for small template changes to let it keep “working”. But in the long run, I think it would be better for Django to deprecate string_if_invalid and use a Jinja-style “undefined variable class”, as I hinted at in the conclusion of that post.
comment:6 by , 14 months ago
| Severity: | Normal → Release blocker |
|---|---|
| Triage Stage: | Accepted → Ready for checkin |
Marking as release blocker since this was a regression in 01ed59f753139afb514170ee7f7384c155ecbc2d.
Hello Fábio Domingues, thank you for your ticket. There was a previous conversation about this in the (unofficial) Django Discord server, I'll extract some fragments to provide context in this ticket for future readers:
From Marijke:
From David:
From me:
We have some previous similar tickets being accepted such as #31865 and #2688, but we also have one closed #28516 in favor of #28526.
Also, I have taken a quick look at your proposed fix (PR) and I'm not sure that is the right fix. I would rather to design a way to be able to pass the prefix from the call sites instead.
Because all of the above, I'm tentatively accepting this ticket to evaluate how a solution would look like, but I also think that if we don't find a clean and nice way to fix this issue in particular, we should close as dupe of #28526.
(Adam, I'm adding you as cc since you added yourself to #28526.)