Django 在 Shell 中无法渲染 Context

57 阅读2分钟

在 Django 中,可以使用 Context 对象将数据传递给模板,然后通过 render() 方法渲染模板。但是,当在 Django Shell 中运行这些代码时,可能会遇到以下错误:

huake_00198_.jpg

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/template/backends/django.py", line 74, in render
    return self.template.render(context)
  File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/template/base.py", line 209, in render
    return self._render(context)
  File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/template/base.py", line 201, in _render
    return self.nodelist.render(context)
  File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/template/base.py", line 903, in render
    bit = self.render_node(node, context)
  File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/template/base.py", line 917, in render_node
    return node.render(context)
  File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/template/base.py", line 963, in render
    return render_value_in_context(output, context)
  File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/template/base.py", line 939, in render_value_in_context
    value = localize(value, use_l10n=context.use_l10n)
  File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/utils/formats.py", line 181, in localize
    return number_format(value, use_l10n=use_l10n)
  File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/utils/formats.py", line 162, in number_format
    get_format('DECIMAL_SEPARATOR', lang, use_l10n=use_l10n),
  File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/utils/formats.py", line 110, in get_format
    for module in get_format_modules(lang):
  File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/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/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/utils/formats.py", line 51, in iter_format_modules
    if not check_for_language(lang):
  File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 181, in check_for_language
    return _trans.check_for_language(lang_code)
  File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/functools.py", line 472, in wrapper
    result = user_function(*args, **kwds)
  File "/Users/croberts/.virtualenvs/testproj/lib/python3.4/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

2、解决方案

有以下两种解决方案:

  1. 升级 Django 版本

此问题已在 Django 1.8.1 中修复,因此您可以升级到最新版本的 Django 以解决此问题。

  1. 在 Shell 中激活语言

如果您不想升级 Django,也可以通过在 Shell 中激活语言来解决此问题:

from django.utils.translation import activate
activate('en')  # or any language code

激活语言后,您就可以在 Shell 中渲染 Context 了。