Opened 5 months ago

Closed 5 months ago

#35571 closed Bug (invalid)

TypeError in assertTemplateUsed when using pathlib.Path as template_name

Reported by: Erik Z. Owned by: nobody
Component: Testing framework Version: 5.0
Severity: Normal Keywords: assertTemplateUsed
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

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

Attachments (1)

Screenshot 2024-07-03 143121.2.png (225.6 KB ) - added by Rish 5 months ago.

Download all attachments as: .zip

Change History (7)

comment:1 by Natalia Bidart, 5 months ago

Component: Template systemTesting framework
Keywords: assertTemplateUsed added; template_name removed
Resolution: needsinfo
Status: newclosed

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.

comment:2 by Erik Z., 5 months ago

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

comment:3 by Erik Z., 5 months ago

Resolution: needsinfo
Status: closednew

comment:4 by Rish, 5 months ago

I tried to reproduce this bug using the repo provided: https://github.com/guserav/django_bug_test_template_name_str/
Can't seem to reproduce it, all tests run just fine on django version 5.0.4. https://code.djangoproject.com/attachment/ticket/35571/Screenshot%202024-07-03%20143121.2.png
Can you please elaborate more on this?

Last edited 5 months ago by Rish (previous) (diff)

comment:5 by Erik Z., 5 months ago

The tests are deliberatly designed to run through. I removed the Exception assertions that made the tests work. Now the tests are failing.

comment:6 by Natalia Bidart, 5 months ago

Resolution: invalid
Status: newclosed

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:

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 Django Forum.

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