Opened 4 months ago

Closed 4 months ago

Last modified 4 months ago

#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 Natalia Bidart, 4 months ago

Cc: Adam Johnson added
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

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:

My changes to fieldsets.html have created an obscure issue. (PR: https://github.com/django/django/pull/17910)
I enabled FAIL_INVALID_TEMPLATE_VARS in pytest-django, which is "Fail tests that render templates which make use of invalid template variables."
I also ran tests on the availability of the admin model "changelist" and "add" pages (just a simple request.get() to see if the pages return HTTP 200).
These tests now fail on the following error: Failed: Undefined template variable 'fieldset.formset.prefix' in 'path/to/django/contrib/admin/templates/admin/includes/fieldset.html'
You don't notice this issue when using the admin, since Django doesn't crash on missing references by default.
Do I report this as a bug in Trac?

From David:

While "Django doesn't crash on missing references by default" is 100% a feature and some/many folk rely on it there are others who will want to test for missing template variables. See blog post by Adam https://adamj.eu/tech/2022/03/30/how-to-make-django-error-for-undefined-template-variables/
I'm not sure it's a bug if the Django admin is using Django's default behaviour. Afterall 'the admin isn't your project' .
Clearly some folk 'abuse' this though.
I'm therefore a unsure if this is worth fixing (I'm slightly leaning towards this tbh).

From me:

while I agree that Django admin is using Django's default behaviour, do we have a confirmation that formset is "correctly" undefined? Marijke do you have more details?

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

comment:2 by Natalia Bidart, 4 months ago

Needs tests: set
Patch needs improvement: set

comment:3 by Natalia Bidart, 4 months ago

Summary: #35189 brokes admin/[model]/add pages when application does not allow undefined variables on templatesUndefined template variables on Admin's add page templates when using fieldsets

comment:4 by Sarah Boyce, 4 months ago

Needs tests: unset
Owner: set to Sarah Boyce
Patch needs improvement: unset
Status: newassigned

comment:5 by Adam Johnson, 4 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 Natalia Bidart, 4 months ago

Severity: NormalRelease blocker
Triage Stage: AcceptedReady for checkin

Marking as release blocker since this was a regression in 01ed59f753139afb514170ee7f7384c155ecbc2d.

comment:7 by Sarah Boyce <42296566+sarahboyce@…>, 4 months ago

Resolution: fixed
Status: assignedclosed

In fd1dd767:

Fixed #35716 -- Fixed VariableDoesNotExist when rendering admin fieldsets.

Regression in 01ed59f753139afb514170ee7f7384c155ecbc2d.

Thank you to Fábio Domingues and Marijke Luttekes for the report,
and thank you to Natalia Bidart for the review.

comment:8 by Sarah Boyce <42296566+sarahboyce@…>, 4 months ago

In 6203965:

[5.1.x] Fixed #35716 -- Fixed VariableDoesNotExist when rendering admin fieldsets.

Regression in 01ed59f753139afb514170ee7f7384c155ecbc2d.

Thank you to Fábio Domingues and Marijke Luttekes for the report,
and thank you to Natalia Bidart for the review.

Backport of fd1dd767783b5a7ec1a594fcc5885e7e4178dd26 from main.

Note: See TracTickets for help on using tickets.
Back to Top