Opened 10 years ago

Closed 10 years ago

#24775 closed Bug (duplicate)

Template.render() throws exception when no language is activated

Reported by: Karmo Rosental Owned by: nobody
Component: Template system Version: 1.8
Severity: Normal Keywords: template, render, translation, now
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I cannot render "now" template tag (and maybe some more tags that I haven't tested).

In [1]: from django.template import Template, Context

In [2]: Template('{% now "Y" %}').render(Context())  
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-c2e400a38725> in <module>()
----> 1 Template('{% now "Y" %}').render(Context())

/usr/local/lib/python3.4/dist-packages/django/template/base.py in render(self, context)
    207             if context.template is None:
    208                 with context.bind_template(self):
--> 209                     return self._render(context)
    210             else:
    211                 return self._render(context)

/usr/local/lib/python3.4/dist-packages/django/template/base.py in _render(self, context)
    199 
    200     def _render(self, context):
--> 201         return self.nodelist.render(context)
    202 
    203     def render(self, context):

/usr/local/lib/python3.4/dist-packages/django/template/base.py in render(self, context)
    901         for node in self:
    902             if isinstance(node, Node):
--> 903                 bit = self.render_node(node, context)
    904             else:
    905                 bit = node

/usr/local/lib/python3.4/dist-packages/django/template/debug.py in render_node(self, node, context)
     77     def render_node(self, node, context):
     78         try:
---> 79             return node.render(context)
     80         except Exception as e:
     81             if not hasattr(e, 'django_template_source'):

/usr/local/lib/python3.4/dist-packages/django/template/defaulttags.py in render(self, context)
    427     def render(self, context):
    428         tzinfo = timezone.get_current_timezone() if settings.USE_TZ else None
--> 429         formatted = date(datetime.now(tz=tzinfo), self.format_string)
    430 
    431         if self.asvar:

/usr/local/lib/python3.4/dist-packages/django/template/defaultfilters.py in date(value, arg)
    769         arg = settings.DATE_FORMAT
    770     try:
--> 771         return formats.date_format(value, arg)
    772     except AttributeError:
    773         try:

/usr/local/lib/python3.4/dist-packages/django/utils/formats.py in date_format(value, format, use_l10n)
    134     be localized (or not), overriding the value of settings.USE_L10N.
    135     """
--> 136     return dateformat.format(value, get_format(format or 'DATE_FORMAT', use_l10n=use_l10n))
    137 
    138 

/usr/local/lib/python3.4/dist-packages/django/utils/formats.py in get_format(format_type, lang, use_l10n)
    108                 return getattr(settings, format_type)
    109         except KeyError:
--> 110             for module in get_format_modules(lang):
    111                 try:
    112                     val = getattr(module, format_type)

/usr/local/lib/python3.4/dist-packages/django/utils/formats.py in get_format_modules(lang, reverse)
     80     if lang is None:
     81         lang = get_language()
---> 82     modules = _format_modules_cache.setdefault(lang, list(iter_format_modules(lang, settings.FORMAT_MODULE_PATH)))
     83     if reverse:
     84         return list(reversed(modules))

/usr/local/lib/python3.4/dist-packages/django/utils/formats.py in iter_format_modules(lang, format_module_path)
     49     Does the heavy lifting of finding format modules.
     50     """
---> 51     if not check_for_language(lang):
     52         return
     53 

/usr/local/lib/python3.4/dist-packages/django/utils/translation/__init__.py in check_for_language(lang_code)
    179 
    180 def check_for_language(lang_code):
--> 181     return _trans.check_for_language(lang_code)
    182 
    183 

/usr/lib/python3.4/functools.py in wrapper(*args, **kwds)
    450                         hits += 1
    451                         return result
--> 452                 result = user_function(*args, **kwds)
    453                 with lock:
    454                     if key in cache:

/usr/local/lib/python3.4/dist-packages/django/utils/translation/trans_real.py in check_for_language(lang_code)
    407     """
    408     # First, a quick check to make sure lang_code is well-formed (#21458)
--> 409     if not language_code_re.search(lang_code):
    410         return False
    411     for path in all_locale_paths():

TypeError: expected string or buffer

It's because get_language() returns None since Django 1.8. I think it should handle the None type somehow and not throw an exception.

If I do translation.acticate('en') for example, then this exception goes away since get_language() returns 'en' then.

Change History (2)

comment:1 by Karmo Rosental, 10 years ago

Type: UncategorizedBug

comment:2 by Karmo Rosental, 10 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #24569.

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