Opened 3 years ago

Last modified 2 years ago

#28172 new Bug

Prevent nonexistent template filter arguments from raising VariableDoesNotExist

Reported by: Vlastimil Zíma Owned by:
Component: Template system Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Someday/Maybe
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


I'd like to fix the error reported in #13167. The main bug

from django.template import Context, Template
Template('{{ foo|default:notreal }}').render(Context({'foo': ''}))

still raises VariableDoesNotExist exception, which is completely unexpected from templates, as noted in closing comment ticket:13167#comment:20. I took me more than an hour before I found out how did it make the server error I investigated.

I know the original ticket was closed as wontfix, but I understood it was due to the effect the proposed change would have on if tag. But there's another way - capture the exception in VariableNode the similar way UnicodeDecodeError is silenced. If I understood the code correctly, that would solve the problem for all template filters, but left template tags intact.

Change History (4)

comment:1 Changed 3 years ago by Tim Graham

Summary: Non-existent arg passed to template filter raises VariableDoesNotExist (alternative solution)Prevent nonexistent template filter arguments from raising VariableDoesNotExist
Triage Stage: UnreviewedSomeday/Maybe

I understand the "template errors don't raise exceptions" philosophy, however, I think changing the current behavior would be more error prone (e.g. typos in variable names go more easily undetected). I've started a django-developers thread to get other opinions.

comment:2 Changed 2 years ago by Alejandro Zamora Fonseca

Owner: changed from nobody to Alejandro Zamora Fonseca
Status: newassigned

comment:3 Changed 2 years ago by Tim Graham

Alejandro, I don't think there's consensus to make this change (hence the ticket's Someday/Maybe status). Did you read the mailing list discussion linked from in comment 1?

comment:4 Changed 2 years ago by Alejandro Zamora Fonseca

Owner: Alejandro Zamora Fonseca deleted
Status: assignednew
Note: See TracTickets for help on using tickets.
Back to Top