[Django] #35522: 'NoneType' object has no attribute 'lstrip' when loading single template with relative include without origin

12 views
Skip to first unread message

Django

unread,
Jun 12, 2024, 7:18:28 AM6/12/24
to django-...@googlegroups.com
#35522: 'NoneType' object has no attribute 'lstrip' when loading single template
with relative include without origin
-------------------------------------------+------------------------
Reporter: Hovi | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+------------------------
Behaviour described by this test:


{{{

from django.template import Template, Origin, TemplateSyntaxError
from django.test import TestCase


class TestInclude(TestCase):
def test_passes(self):
Template(
"""
{% include "question.html" %}
"""
)

def test_also_passes(self):
Template(
"""
{% include "./question.html" %}
""",
origin=Origin(
name="doesntmatter.html", template_name="need-to-be-
set.html"
),
)

def test_fails_correctly(self):
with self.assertRaises(TemplateSyntaxError) as cm:
Template(
"""
{% include "../question.html" %}
""",
origin=Origin(
name="doesntmatter.html", template_name="need-to-be-
set.html"
),
)
self.assertIn("points outside the file hierarchy that
template", str(cm.exception))

def test_fails(self):
with self.assertRaises(AttributeError) as cm:
Template(
"""
{% include "./question.html" %}
"""
)
self.assertIn("'NoneType' object has no attribute 'lstrip'",
str(cm.exception))

def test_fails_on_incorrect_error(self):
with self.assertRaises(AttributeError) as cm:
Template(
"""
{% include "../question.html" %}
"""
)
self.assertIn("'NoneType' object has no attribute 'lstrip'",
str(cm.exception))
--
Ticket URL: <https://code.djangoproject.com/ticket/35522>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 12, 2024, 11:07:28 AM6/12/24
to django-...@googlegroups.com
#35522: 'NoneType' object has no attribute 'lstrip' when loading single template
with relative include without origin
---------------------------------+--------------------------------------
Reporter: Hovi | Owner: nobody
Type: Bug | Status: closed
Component: Template system | Version: 5.0
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by Sarah Boyce):

* resolution: => needsinfo
* status: new => closed

Comment:

Hi Hovi, thank you for this report and the tests! I can see what you're
pointing out. 👍
It is not clear to me why someone would want to do this and so I am not
sure whether to accept this as an issue yet.

I think one way we could approach this would be to have
[https://docs.djangoproject.com/en/5.0/ref/templates/api/#django.template.base.Origin.template_name
Origin.template_name] default to `<unknown_template>` similar to the
`<unknown_source>` default. However, that is a change to documented
behaviour and would need a discussion and have others agree that this is
the best approach.

Can you start a discussion on the
[https://forum.djangoproject.com/c/internals/5 Django forum] and see
whether others agree that this is 1) an issue and 2) how best to approach
resolving this?
--
Ticket URL: <https://code.djangoproject.com/ticket/35522#comment:1>

Django

unread,
Jun 12, 2024, 11:41:16 AM6/12/24
to django-...@googlegroups.com
#35522: 'NoneType' object has no attribute 'lstrip' when loading single template
with relative include without origin
---------------------------------+--------------------------------------
Reporter: Hovi | Owner: nobody
Type: Bug | Status: closed
Component: Template system | Version: 5.0
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Comment (by Hovi):

Thanks! Started discussion here:
https://forum.djangoproject.com/t/is-this-a-template-bug-or-not/32024

To add extra background info - I came across this when I wanted to build
simple management command, that goes through all my templates and just
parses them and fail in case any of them don't parse to be run on CI
server as part of our build. It works, if I just pass relative path of
template as origin.template_name
Some of our frontend developers tend to forget quotes and things in
includes or translation tags and some pages don't get caught by other
tests.
Maybe there's better way to do this kind of check or better that I missed.
--
Ticket URL: <https://code.djangoproject.com/ticket/35522#comment:2>
Reply all
Reply to author
Forward
0 new messages