Opened 4 months ago
Closed 4 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)
Change History (7)
comment:1 by , 4 months ago
Component: | Template system → Testing framework |
---|---|
Keywords: | assertTemplateUsed added; template_name removed |
Resolution: | → needsinfo |
Status: | new → closed |
comment:2 by , 4 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 , 4 months ago
Resolution: | needsinfo |
---|---|
Status: | closed → new |
by , 4 months ago
Attachment: | Screenshot 2024-07-03 143121.2.png added |
---|
comment:4 by , 4 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.
Can you please elaborate more on this?
comment:5 by , 4 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 , 4 months ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
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:
- django.template.loader "This function loads the template with the given name and returns a Template object."
- 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 Django Forum.
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.