Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#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


This code:

from django.template import Context, Template
import datetime
t = Template('{{ d|date:"Y" }}')
c = Context({'d'})

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/", line 209, in render
    return self._render(context)
  File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/template/", line 201, in _render
    return self.nodelist.render(context)
  File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/template/", line 903, in render
    bit = self.render_node(node, context)
  File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/template/", line 917, in render_node
    return node.render(context)
  File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/template/", line 957, in render
    output = self.filter_expression.resolve(context)
  File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/template/", line 674, in resolve
    new_obj = func(obj, *arg_vals)
  File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/template/", line 771, in date
    return formats.date_format(value, arg)
  File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/utils/", 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/", 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/", 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/", 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/", 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/", line 125, in wrapper
    result = user_function(*args, **kwds)
  File "/Users/Nikso/Work/Antlos/AntlosServer/virtual/lib/python2.7/site-packages/django/utils/translation/", line 409, in check_for_language
    if not
TypeError: expected string or buffer

Change History (8)

comment:1 by Baptiste Mispelon, 9 years ago


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


comment:2 by Marten Kenbeek, 9 years ago

Triage Stage: UnreviewedAccepted

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.

Version 0, edited 9 years ago by Marten Kenbeek (next)

comment:3 by Baptiste Mispelon, 9 years ago

OK, I can now reproduce this (the trick is to execute the code in a ./ 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.


comment:4 by Claude Paroz, 9 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/

    diff --git a/django/utils/translation/ b/django/utils/translation/
    index ff84589..bebe69f 100644
    a b def check_for_language(lang_code):  
    406406    <>.
    407407    """
    408408    # First, a quick check to make sure lang_code is well-formed (#21458)
    409     if not
     409    if lang_code is None or not
    410410        return False
    411411    for path in all_locale_paths():
    412412        if gettext_module.find('django', path, [to_locale(lang_code)]) is not None:

Test needed, of course.

comment:5 by Claude Paroz, 9 years ago

Has patch: set

comment:6 by Tim Graham, 9 years ago

Triage Stage: AcceptedReady for checkin

comment:7 by Claude Paroz <claude@…>, 9 years ago

Resolution: fixed
Status: newclosed

In 7a0d9b5:

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.

comment:8 by Claude Paroz <claude@…>, 9 years ago

In 3a76276:

[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.

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