[Django] #24569: Template `date` filter fails with TypeError

26 views
Skip to first unread message

Django

unread,
Apr 2, 2015, 1:46:43 PM4/2/15
to django-...@googlegroups.com
#24569: Template `date` filter fails with TypeError
---------------------------------+--------------------
Reporter: thenikso | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Release blocker | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------
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
}}}

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

Django

unread,
Apr 2, 2015, 1:53:20 PM4/2/15
to django-...@googlegroups.com
#24569: Template `date` filter fails with TypeError
---------------------------------+--------------------------------------

Reporter: thenikso | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by bmispelon):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Hi,

That code works for me. Can you show the content of your settings (in
particular, anything related to i18n)?

Thanks.

--
Ticket URL: <https://code.djangoproject.com/ticket/24569#comment:1>

Django

unread,
Apr 2, 2015, 2:06:12 PM4/2/15
to django-...@googlegroups.com
#24569: Template `date` filter fails with TypeError
---------------------------------+------------------------------------

Reporter: thenikso | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by knbk):

* stage: Unreviewed => Accepted


Comment:

To reproduce, start a new project using Django's project template. I
believe that `USE_L10N = True` is the issue, though it might be a
combination of settings that causes it.

--
Ticket URL: <https://code.djangoproject.com/ticket/24569#comment:2>

Django

unread,
Apr 2, 2015, 4:51:37 PM4/2/15
to django-...@googlegroups.com
#24569: Template `date` filter fails with TypeError
---------------------------------+------------------------------------

Reporter: thenikso | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

Comment (by bmispelon):

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

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

Django

unread,
Apr 2, 2015, 5:25:42 PM4/2/15
to django-...@googlegroups.com
#24569: Template `date` filter fails with TypeError
---------------------------------+------------------------------------

Reporter: thenikso | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

Comment (by claudep):

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:
{{{
#!diff
diff --git a/django/utils/translation/trans_real.py
b/django/utils/translation/trans_real.py
index ff84589..bebe69f 100644
--- a/django/utils/translation/trans_real.py
+++ b/django/utils/translation/trans_real.py
@@ -406,7 +406,7 @@ def check_for_language(lang_code):
<https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
"""
# First, a quick check to make sure lang_code is well-formed (#21458)
- if not language_code_re.search(lang_code):
+ if lang_code is None or not language_code_re.search(lang_code):
return False
for path in all_locale_paths():
if gettext_module.find('django', path, [to_locale(lang_code)]) is
not None:
}}}
Test needed, of course.

--
Ticket URL: <https://code.djangoproject.com/ticket/24569#comment:4>

Django

unread,
Apr 3, 2015, 6:17:40 AM4/3/15
to django-...@googlegroups.com
#24569: Template `date` filter fails with TypeError
---------------------------------+------------------------------------

Reporter: thenikso | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by claudep):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/24569#comment:5>

Django

unread,
Apr 3, 2015, 7:34:17 AM4/3/15
to django-...@googlegroups.com
#24569: Template `date` filter fails with TypeError
-------------------------------------+-------------------------------------

Reporter: thenikso | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 1.8
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/24569#comment:6>

Django

unread,
Apr 4, 2015, 4:56:03 AM4/4/15
to django-...@googlegroups.com
#24569: Template `date` filter fails with TypeError
-------------------------------------+-------------------------------------
Reporter: thenikso | Owner: nobody
Type: Bug | Status: closed

Component: Template system | Version: 1.8
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Claude Paroz <claude@…>):

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


Comment:

In [changeset:"7a0d9b5cda04c74287e052f1b4ebdf869e53ff0f" 7a0d9b5]:
{{{
#!CommitTicketReference repository=""
revision="7a0d9b5cda04c74287e052f1b4ebdf869e53ff0f"
Fixed #24569 -- Made some translation functions accept None value

get_language() can return None when translations are deactivated.
Thanks Nicola Peduzzi for the reporti and Tim Graham for the review.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24569#comment:7>

Django

unread,
Apr 4, 2015, 4:59:57 AM4/4/15
to django-...@googlegroups.com
#24569: Template `date` filter fails with TypeError
-------------------------------------+-------------------------------------
Reporter: thenikso | Owner: nobody
Type: Bug | Status: closed

Component: Template system | Version: 1.8
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Claude Paroz <claude@…>):

In [changeset:"3a762762121acfc83f0659c1f1955aee33bc9804" 3a76276]:
{{{
#!CommitTicketReference repository=""
revision="3a762762121acfc83f0659c1f1955aee33bc9804"
[1.8.x] Fixed #24569 -- Made some translation functions accept None value

get_language() can return None when translations are deactivated.
Thanks Nicola Peduzzi for the reporti and Tim Graham for the review.

Backport of 7a0d9b5cda from master.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24569#comment:8>

Reply all
Reply to author
Forward
0 new messages