[Django] #35571: TypeError in assertTemplateUsed when using pathlib.Path as template_name

15 views
Skip to first unread message

Django

unread,
Jul 1, 2024, 4:11:30 PM (4 days ago) Jul 1
to django-...@googlegroups.com
#35571: TypeError in assertTemplateUsed when using pathlib.Path as template_name
-------------------------------------------+-------------------------------
Reporter: Erik Z. | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 5.0
Severity: Normal | Keywords: template_name
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------------+-------------------------------
Right now it is perfectly fine to use pathlib.Path as an template_name.
This works fine for production as the template render fine. However when
unit testing the assertTemplateUsed function throws a TypeError:

{{{
File "<stuff>/.local/lib/python3.12/site-
packages/django/test/testcases.py", line 681, in _assert_template_used
% (template_name, ", ".join(template_names)),
^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: sequence item 0: expected str instance, PosixPath found
}}}

This happens because the template_name is stored as is.

Happened on 5.0.4
--
Ticket URL: <https://code.djangoproject.com/ticket/35571>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 1, 2024, 5:57:11 PM (4 days ago) Jul 1
to django-...@googlegroups.com
#35571: TypeError in assertTemplateUsed when using pathlib.Path as template_name
------------------------------------+--------------------------------------
Reporter: Erik Z. | Owner: nobody
Type: Bug | Status: closed
Component: Testing framework | Version: 5.0
Severity: Normal | Resolution: needsinfo
Keywords: assertTemplateUsed | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
------------------------------------+--------------------------------------
Changes (by Natalia Bidart):

* component: Template system => Testing framework
* keywords: template_name => assertTemplateUsed
* resolution: => needsinfo
* status: new => closed

Comment:

Hello Erik! Thank you for your report.

The PR you have submitted is not ideal in that the "production" code is
coercing to `str` in multiple places and this could impact performance.
Following your description, and ideally, we should provide a fix within
the Testing framework.

To fully understand this issue, I would need a minimal django project
(views and tests) so we can reproduce this, triage it and review the fix
accordingly.
--
Ticket URL: <https://code.djangoproject.com/ticket/35571#comment:1>

Django

unread,
Jul 2, 2024, 6:40:11 PM (3 days ago) Jul 2
to django-...@googlegroups.com
#35571: TypeError in assertTemplateUsed when using pathlib.Path as template_name
------------------------------------+--------------------------------------
Reporter: Erik Z. | Owner: nobody
Type: Bug | Status: closed
Component: Testing framework | Version: 5.0
Severity: Normal | Resolution: needsinfo
Keywords: assertTemplateUsed | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
------------------------------------+--------------------------------------
Comment (by Erik Z.):

Sure, I can understand that.
I prepared a project which should give an understanding on how the error
works.
It can be found here:
https://github.com/guserav/django_bug_test_template_name_str/
all important code should be in
https://github.com/guserav/django_bug_test_template_name_str/commit/828d56b405c7932fdcf6c6c29a9038ce115628ed
--
Ticket URL: <https://code.djangoproject.com/ticket/35571#comment:2>

Django

unread,
Jul 2, 2024, 6:40:20 PM (3 days ago) Jul 2
to django-...@googlegroups.com
#35571: TypeError in assertTemplateUsed when using pathlib.Path as template_name
------------------------------------+--------------------------------------
Reporter: Erik Z. | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 5.0
Severity: Normal | Resolution:
Keywords: assertTemplateUsed | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
------------------------------------+--------------------------------------
Changes (by Erik Z.):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/35571#comment:3>

Django

unread,
Jul 3, 2024, 5:02:29 AM (3 days ago) Jul 3
to django-...@googlegroups.com
#35571: TypeError in assertTemplateUsed when using pathlib.Path as template_name
------------------------------------+--------------------------------------
Reporter: Erik Z. | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 5.0
Severity: Normal | Resolution:
Keywords: assertTemplateUsed | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
------------------------------------+--------------------------------------
Changes (by Rish):

* Attachment "Screenshot 2024-07-03 143121.png" added.

Django

unread,
Jul 3, 2024, 5:03:05 AM (3 days ago) Jul 3
to django-...@googlegroups.com
#35571: TypeError in assertTemplateUsed when using pathlib.Path as template_name
------------------------------------+--------------------------------------
Reporter: Erik Z. | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 5.0
Severity: Normal | Resolution:
Keywords: assertTemplateUsed | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
------------------------------------+--------------------------------------
Changes (by Rish):

* Attachment "Screenshot 2024-07-03 143121.2.png" added.

Django

unread,
Jul 3, 2024, 5:04:03 AM (3 days ago) Jul 3
to django-...@googlegroups.com
#35571: TypeError in assertTemplateUsed when using pathlib.Path as template_name
------------------------------------+--------------------------------------
Reporter: Erik Z. | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 5.0
Severity: Normal | Resolution:
Keywords: assertTemplateUsed | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
------------------------------------+--------------------------------------
Changes (by Rish):

* Attachment "Screenshot 2024-07-03 143121.png" removed.

Django

unread,
Jul 3, 2024, 5:07:58 AM (3 days ago) Jul 3
to django-...@googlegroups.com
#35571: TypeError in assertTemplateUsed when using pathlib.Path as template_name
------------------------------------+--------------------------------------
Reporter: Erik Z. | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 5.0
Severity: Normal | Resolution:
Keywords: assertTemplateUsed | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
------------------------------------+--------------------------------------
Comment (by Rish):

I tried to reproduce this bug using the repo provided:
[https://github.com/guserav/django_bug_test_template_name_str/]\\
Can't seen to reproduce it, all tests run just fine on django version
5.0.4.
[[Image(https://code.djangoproject.com/attachment/ticket/35571/Screenshot%202024-07-03%20143121.2.png)]]\\
Can you please elaborate more on this?
--
Ticket URL: <https://code.djangoproject.com/ticket/35571#comment:4>

Django

unread,
Jul 3, 2024, 1:25:01 PM (2 days ago) Jul 3
to django-...@googlegroups.com
#35571: TypeError in assertTemplateUsed when using pathlib.Path as template_name
------------------------------------+--------------------------------------
Reporter: Erik Z. | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 5.0
Severity: Normal | Resolution:
Keywords: assertTemplateUsed | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
------------------------------------+--------------------------------------
Comment (by Erik Z.):

The tests are deliberatly designed to run through. I removed the Exception
assertions that made the tests work. Now the tests are failing.
--
Ticket URL: <https://code.djangoproject.com/ticket/35571#comment:5>

Django

unread,
10:40 AM (8 hours ago) 10:40 AM
to django-...@googlegroups.com
#35571: TypeError in assertTemplateUsed when using pathlib.Path as template_name
------------------------------------+--------------------------------------
Reporter: Erik Z. | Owner: nobody
Type: Bug | Status: closed
Component: Testing framework | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: assertTemplateUsed | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
------------------------------------+--------------------------------------
Changes (by Natalia Bidart):

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

Comment:

Hello Erik, thank you for the sample Django project; it's been helpful.

Now that I understand your issue, after reviewing the documentation
thoroughly, I believe the problem stems from a potential misunderstanding
of how the template name should be defined. You are using pathlib to
define the template name, suggesting that the value could be a Path
instance. However, the Django template system expects the template name to
be a string identifier.

If you refer to these sections in the documentation:

* [https://docs.djangoproject.com/en/dev/topics/templates/#module-
django.template.loader django.template.loader] "This function loads the
template with the given name and returns a Template object."
*
[https://docs.djangoproject.com/en/dev/topics/templates/#django.template.loader.render_to_string
render_to_string] "template_name: The name of the template to load and
render"

The `template_name` should be a string, not a `Path` instance. It serves
as an identifier for the template rather than a direct path in the
filesystem. Each template loader then performs the necessary
transformations or lookups using this identifier to find the requested
template, utilizing whichever storage mechanism they are configured with
(e.g., in-memory cache, filesystem, network-based storage, etc).

I'll be closing this ticket accordingly, if you have further questions
about this topic, please reach out to the Django community in the
[https://forum.djangoproject.com/ Django Forum].
--
Ticket URL: <https://code.djangoproject.com/ticket/35571#comment:6>
Reply all
Reply to author
Forward
0 new messages