#24569 closed Bug (fixed)
Template `date` filter fails with TypeError
Reported by: | Nicola Peduzzi | Owned by: | nobody |
---|---|---|---|
Component: | Template system | Version: | 1.8 |
Severity: | Release blocker | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This code:
from django.template import Context, Template import datetime t = Template('{{ d|date:"Y" }}') c = Context({'d':datetime.datetime.now()}) t.render(c)
Fails with:
Traceback (most recent call last): File "<console>", line 1, in <module> File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/template/base.py", line 209, in render return self._render(context) File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/template/base.py", line 201, in _render return self.nodelist.render(context) File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/template/base.py", line 903, in render bit = self.render_node(node, context) File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/template/base.py", line 917, in render_node return node.render(context) File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/template/base.py", line 957, in render output = self.filter_expression.resolve(context) File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/template/base.py", line 674, in resolve new_obj = func(obj, *arg_vals) File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/template/defaultfilters.py", line 771, in date return formats.date_format(value, arg) File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/utils/formats.py", line 136, in date_format return dateformat.format(value, get_format(format or 'DATE_FORMAT', use_l10n=use_l10n)) File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/utils/formats.py", line 110, in get_format for module in get_format_modules(lang): File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/utils/formats.py", line 82, in get_format_modules modules = _format_modules_cache.setdefault(lang, list(iter_format_modules(lang, settings.FORMAT_MODULE_PATH))) File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/utils/formats.py", line 51, in iter_format_modules if not check_for_language(lang): File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 181, in check_for_language return _trans.check_for_language(lang_code) File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/utils/lru_cache.py", line 125, in wrapper result = user_function(*args, **kwds) File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 409, in check_for_language if not language_code_re.search(lang_code): TypeError: expected string or buffer
Change History (8)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
Triage Stage: | Unreviewed → Accepted |
---|
To reproduce, start a new project using Django's project template. I believe that USE_L10N = True
exposes the issue, though it might be a combination of settings that causes it.
comment:3 by , 10 years ago
OK, I can now reproduce this (the trick is to execute the code in a ./manage.py shell
).
I've tracked down the regression (this code used to work in 1.7) to commit 543df07720181fe23737ba14f1a261ff6f37f49c.
As knbk pointed out on IRC, the problem seems to be that translation.get_language()
returns None
because the BaseManagementCommand
calls deactivate_all
[1]
Even though it's a regression, I'm not sure it should be treated as a release blocker because I fail to see a valid use-case where this change would cause a problem.
[1] https://github.com/django/django/blob/master/django/core/management/base.py#L391
comment:4 by , 10 years ago
Surely, the fact that get_language
might return None
is new in 1.8, and it appears that some functions (like check_for_language
in this issue) are not prepared to receive a None
language. Still, I think that returning None
when no language is activated is the right thing. I only hope we'll not have to patch too much Django internals for that...
To fix this one, the following patch might be sufficient:
-
django/utils/translation/trans_real.py
diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index ff84589..bebe69f 100644
a b def check_for_language(lang_code): 406 406 <https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>. 407 407 """ 408 408 # First, a quick check to make sure lang_code is well-formed (#21458) 409 if not language_code_re.search(lang_code):409 if lang_code is None or not language_code_re.search(lang_code): 410 410 return False 411 411 for path in all_locale_paths(): 412 412 if gettext_module.find('django', path, [to_locale(lang_code)]) is not None:
Test needed, of course.
comment:5 by , 10 years ago
Has patch: | set |
---|
comment:6 by , 10 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Hi,
That code works for me. Can you show the content of your settings (in particular, anything related to i18n)?
Thanks.