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 Pravin, 8 weeks ago

Cc: Pravin added

comment:2 by Shubh Rai, 7 weeks ago

Cc: Shubh Rai added
Has patch: set
Owner: set to Shubh Rai
Status: newassigned

comment:3 by Aniket Gupta, 7 weeks ago

Triage Stage: UnreviewedAccepted

Hi Lily, thanks for catching and reporting this!

I can confirm this bug is still present on the main branch. 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 of django-rusty-templates. As reported, evaluating Template('{% firstof as foo %}') returns an empty node instead of raising the expected TemplateSyntaxError.

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!

Last edited 7 weeks ago by Aniket Gupta (previous) (diff)

comment:4 by Shubh Rai, 7 weeks ago

I have opened a pr:https://github.com/django/django/pull/20978, Please let me know if any changes are required.

comment:5 by Tim McCurrach, 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.

in reply to:  5 ; comment:6 by Shubh Rai, 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?

in reply to:  6 ; comment:7 by Tim McCurrach, 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:8 by Shubh Rai, 6 weeks ago

okay I will wait till then.

in reply to:  7 comment:9 by Shubh Rai, 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 Vaibhav Pant, 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!

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