{{{
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.
* 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>
* 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>
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>
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>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/24569#comment:5>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/24569#comment:6>
* 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>
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>