Opened 8 weeks ago
Last modified 2 days ago
#36998 assigned Bug
firstof tag doesn't require an argument when used with as var
| Reported by: | Lily | Owned by: | Shubh Rai |
|---|---|---|---|
| Component: | Template system | Version: | 6.0 |
| Severity: | Normal | Keywords: | |
| Cc: | Pravin, Shubh Rai | Triage Stage: | Accepted |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | yes |
| Easy pickings: | yes | UI/UX: | no |
Description
The {% firstof %} template tag raises a TemplateSyntaxError("'firstof' statement requires at least one argument") when called with no arguments.
However, when called with exactly two arguments that match the variable capturing syntax (e.g. {% firstof as foo %}) no error is raised and the FirstOfNode is returned with an empty vars list (and an asvar of e.g. foo). It would be consistent to raise a TemplateSyntaxError("'firstof' statement requires at least one argument before 'as'") here.
This was discovered when re-implementing `firstof` in django-rusty-templates.
Change History (10)
comment:1 by , 8 weeks ago
| Cc: | added |
|---|
comment:2 by , 7 weeks ago
| Cc: | added |
|---|---|
| Has patch: | set |
| Owner: | set to |
| Status: | new → assigned |
comment:3 by , 7 weeks ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:4 by , 7 weeks ago
I have opened a pr:https://github.com/django/django/pull/20978, Please let me know if any changes are required.
follow-up: 6 comment:5 by , 6 weeks ago
| Patch needs improvement: | set |
|---|
I'd probably also close the existing PR - on the basis that it was done by AI as part of a massive batch of PRs and so doesn't meet our contributing guidelines, and is unlikely to be revisited by the author.
follow-up: 7 comment:6 by , 6 weeks ago
my earlier pr was closed since another contributor was already working on this issue.Given the recent discussion about the existing PR would it be okay for me to reopen or submit an updated PR?
follow-up: 9 comment:7 by , 6 weeks ago
Replying to Shubh Rai:
my earlier pr was closed since another contributor was already working on this issue.Given the recent discussion about the existing PR would it be okay for me to reopen or submit an updated PR?
I'm sure this will be fine - but it's a good idea to wait until either the previous PR has been closed, or a long enough time has passed with no activity (think at least a month or two).
comment:9 by , 2 weeks ago
Its been almost a month without the activity of the previous contributor , can I work on it and open a pr?
comment:10 by , 2 days ago
Hey , I'm a beginner open source contributor and I've been looking into this ticket. I went through the previous PRs and the review comments to understand what went wrong earlier.
I've implemented the fix — adding a check inside the as parsing block that raises a TemplateSyntaxError with the message "'firstof' statement requires at least one argument before 'as'" when no variables are provided before as.
I also added a regression test following the same @setup pattern used in the existing test file, taking reference from how the earlier patch approached testing. All 17 tests are passing locally.
I would love to open a PR for this if it's okay. Please let me know if there's anything I should fix or change before I do!
Hi Lily, thanks for catching and reporting this!
I can confirm this bug is still present on the
mainbranch. I was able to isolate and reproduce the silent failure using a minimal, standalone Django script, confirming that this is a core parser issue and independent ofdjango-rusty-templates. As reported, evaluatingTemplate('{% firstof as foo %}')returns an empty node instead of raising the expectedTemplateSyntaxError.I am upgrading the Triage Stage to 'Accepted' and I would be very happy to write the patch and submit a PR for the fix!