Opened 3 months ago

Closed 3 months ago

Last modified 3 months ago

#35522 closed Bug (needsinfo)

'NoneType' object has no attribute 'lstrip' when loading single template with relative include without origin

Reported by: Hovi Owned by: nobody
Component: Template system Version: 5.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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))

Change History (2)

comment:1 by Sarah Boyce, 3 months ago

Resolution: needsinfo
Status: newclosed

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 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 Django forum and see whether others agree that this is 1) an issue and 2) how best to approach resolving this?

comment:2 by Hovi, 3 months ago

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.

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