#12064 closed New feature (fixed)
Impossible to conditionally include potentially non-existant templates
Reported by: | mkruisselbrink | Owned by: | nobody |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | FunkyBob | 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
It is not possible to conditionally include one template in another template, if the template to include might not exist. The include tag will raise an exception if the template does not exist (and is specified as a string literal, not a variable) even if the actual include tag will never be rendered because a surrounding if statement has a false condition. This can easily be fixed by moving the get_template call from ConstantIncludeNode.__init__
to it's render method.
Attachments (1)
Change History (18)
comment:1 by , 15 years ago
Component: | Uncategorized → Template system |
---|
by , 15 years ago
Attachment: | includefix.diff added |
---|
comment:2 by , 15 years ago
Has patch: | set |
---|
comment:3 by , 15 years ago
Needs tests: | set |
---|---|
Owner: | changed from | to
Status: | new → assigned |
It should be quite easy to produce a test for this bug - no exception should be raised for a template like this:
{% if some_var_that_equals_False %} {% include "some_template_that_does_not_exist.html" %} {% endif %}
That test is needed before the patch can go in.
comment:5 by , 15 years ago
Owner: | changed from | to
---|---|
Status: | assigned → new |
comment:6 by , 15 years ago
The proper fix, assuming this is deemed a valid bug, would be to remove ConstantIncludeNode
altogether, not make it work near identically to IncludeNode
.
comment:7 by , 15 years ago
Triage Stage: | Unreviewed → Design decision needed |
---|
I think this is somewhat related to #12008 - it all hinges on the question of how exactly an {% include %} should be processed -- is it a parsing-level include, or is it a tag that is only evaluated if and when the IncludeNode is rendered?
comment:8 by , 14 years ago
Ticket #3544 is related too (Fix {% include %} to allow recursive includes) and could be solved at the same time.
comment:9 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → New feature |
comment:10 by , 14 years ago
Easy pickings: | unset |
---|
See also #16147
I believe that the behaviour of {% include %}
as it stands is both inconsistent between development (TEMPLATE_DEBUG = True
) and production environments, but also within the tag itself depending on the type of argument passed to it. These inconsistencies have shown themselves in a few different buggy (or at least unexpected) ways that are not always easy to trace.
If including a non-existent template is worth raising TemplateDoesNotExist in a development environment, it should not fail silently in a production environment. The exception should also be raised there and redirected to admins via Django's logging.
If including a non-existent template should fail silently when the template does not exist, it should do so both for includes that have a literal string argument or a variable, and it should do so both in development and production environments.
I don't really have a firm opinion on whether or not including missing templates should always fail silently or loudly, although I believe the most common and expected behaviour currently is to fail silently so there may be backwards incompatible issues in changing that. But I do believe that the {% include %}
tag should work it's magic at render time for both literal and variable arguments. I consider the currently (undocumented) difference in behaviour to be buggy or at the very least, surprising.
comment:11 by , 14 years ago
UI/UX: | unset |
---|
I'd very much like to see this applied and am willing to contribute to make it happen. We're relying on the silent failure in production, but it's a hassle to comment out the includes when developing in debug mode.
I see that the triage stage for #3544 is "Accepted", and #16147 is "Design decision needed". Would one or both of those fix this issue as well? Should I just write tests for this ticket as lukeplant suggested, or should I direct my efforts at one of the related tickets?
#12008 has been turned into a documentation patch, so it won't help here.
comment:12 by , 13 years ago
I added a patch to #16147.
It also addresses the problem of this patch, and lets non-existent templates be conditionally included, even when TEMPLATE_DEBUG is true.
comment:13 by , 12 years ago
Needs tests: | unset |
---|
Updated the patch and added a pull request for review.
comment:14 by , 12 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
This is basically a dup of #16147, especially now that there's a patch addressing 'em both at once.
comment:16 by , 11 years ago
Triage Stage: | Design decision needed → Ready for checkin |
---|---|
Version: | 1.1 → master |
comment:17 by , 11 years ago
Resolution: | duplicate → fixed |
---|
patch that fixes this bug