Django升级1.9.6出现的中文本地化bug

Error日志:

  1. Error opening file for reading: Permission denied  
  2. ERROR Internal Server Error: /  
  3. Traceback (most recent call last):  
  4.   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 174, in get_response  
  5.     response = self.process_exception_by_middleware(e, request)  
  6.   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 172, in get_response  
  7.     response = response.render()  
  8.   File "/usr/local/lib/python2.7/dist-packages/django/template/response.py", line 160, in render  
  9.     self.content = self.rendered_content  
  10.   File "/usr/local/lib/python2.7/dist-packages/django/template/response.py", line 137, in rendered_content  
  11.     content = template.render(context, self._request)  
  12.   File "/usr/local/lib/python2.7/dist-packages/django/template/backends/django.py", line 95, in render  
  13.     return self.template.render(context)  
  14.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 206, in render  
  15.     return self._render(context)  
  16.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 197, in _render  
  17.     return self.nodelist.render(context)  
  18.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 992, in render  
  19.     bit = node.render_annotated(context)  
  20.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 959, in render_annotated  
  21.     return self.render(context)  
  22.   File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 173, in render  
  23.     return compiled_parent._render(context)  
  24.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 197, in _render  
  25.     return self.nodelist.render(context)  
  26.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 992, in render  
  27.     bit = node.render_annotated(context)  
  28.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 959, in render_annotated  
  29.     return self.render(context)  
  30.   File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 69, in render  
  31.     result = block.nodelist.render(context)  
  32.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 992, in render  
  33.     bit = node.render_annotated(context)  
  34.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 959, in render_annotated  
  35.     return self.render(context)  
  36.   File "/usr/local/lib/python2.7/dist-packages/django/template/library.py", line 243, in render  
  37.     return t.render(new_context)  
  38.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 208, in render  
  39.     return self._render(context)  
  40.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 197, in _render  
  41.     return self.nodelist.render(context)  
  42.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 992, in render  
  43.     bit = node.render_annotated(context)  
  44.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 959, in render_annotated  
  45.     return self.render(context)  
  46.   File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 220, in render  
  47.     nodelist.append(node.render_annotated(context))  
  48.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 959, in render_annotated  
  49.     return self.render(context)  
  50.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 1043, in render  
  51.     output = self.filter_expression.resolve(context)  
  52.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 736, in resolve  
  53.     new_obj = func(obj, *arg_vals)  
  54.   File "/usr/local/lib/python2.7/dist-packages/django/template/defaultfilters.py", line 51, in _dec  
  55.     return func(*args, **kwargs)  
  56.   File "/usr/local/lib/python2.7/dist-packages/django/template/defaultfilters.py", line 278, in truncatechars  
  57.     return Truncator(value).chars(length)  
  58.   File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 194, in wrapper  
  59.     return func(*args, **kwargs)  
  60.   File "/usr/local/lib/python2.7/dist-packages/django/utils/text.py", line 103, in chars  
  61.     for char in self.add_truncation_text('', truncate):  
  62.   File "/usr/local/lib/python2.7/dist-packages/django/utils/text.py", line 77, in add_truncation_text  
  63.     '%(truncated_text)s...')  
  64.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 93, in pgettext  
  65.     return _trans.pgettext(context, message)  
  66.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 326, in pgettext  
  67.     result = ugettext(msg_with_ctxt)  
  68.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 321, in ugettext  
  69.     return do_translate(message, 'ugettext')  
  70.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 298, in do_translate  
  71.     _default = _default or translation(settings.LANGUAGE_CODE)  
  72.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 205, in translation  
  73.     _translations[language] = DjangoTranslation(language)  
  74.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 118, in __init__  
  75.     raise IOError("No translation files found for default language %s." % settings.LANGUAGE_CODE)  
  76. IOError: No translation files found for default language zh-CN.  
  77. Traceback (most recent call last):  
  78.   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 177, in __call__  
  79.     response = self.get_response(request)  
  80.   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 230, in get_response  
  81.     response = self.handle_uncaught_exception(request, resolver, sys.exc_info())  
  82.   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 284, in handle_uncaught_exception  
  83.     'request': request  
  84.   File "/usr/lib/python2.7/logging/__init__.py", line 1166, in error  
  85.     self._log(ERROR, msg, args, **kwargs)  
  86.   File "/usr/lib/python2.7/logging/__init__.py", line 1258, in _log  
  87.     self.handle(record)  
  88.   File "/usr/lib/python2.7/logging/__init__.py", line 1268, in handle  
  89.     self.callHandlers(record)  
  90.   File "/usr/lib/python2.7/logging/__init__.py", line 1308, in callHandlers  
  91.     hdlr.handle(record)  
  92.   File "/usr/lib/python2.7/logging/__init__.py", line 748, in handle  
  93.     self.emit(record)  
  94.   File "/usr/local/lib/python2.7/dist-packages/django/utils/log.py", line 115, in emit  
  95.     message = "%s %s" % (self.format(no_exc_record), reporter.get_traceback_text())  
  96.   File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 323, in get_traceback_text  
  97.     return t.render(c)  
  98.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 206, in render  
  99.     return self._render(context)  
  100.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 197, in _render  
  101.     return self.nodelist.render(context)  
  102.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 992, in render  
  103.     bit = node.render_annotated(context)  
  104.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 959, in render_annotated  
  105.     return self.render(context)  
  106.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 1043, in render  
  107.     output = self.filter_expression.resolve(context)  
  108.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 736, in resolve  
  109.     new_obj = func(obj, *arg_vals)  
  110.   File "/usr/local/lib/python2.7/dist-packages/django/template/defaultfilters.py", line 778, in date  
  111.     return formats.date_format(value, arg)  
  112.   File "/usr/local/lib/python2.7/dist-packages/django/utils/formats.py", line 151, in date_format  
  113.     return dateformat.format(value, get_format(format or 'DATE_FORMAT', use_l10n=use_l10n))  
  114.   File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 367, in format  
  115.     return df.format(format_string)  
  116.   File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 37, in format  
  117.     pieces.append(force_text(getattr(self, piece)()))  
  118.   File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 292, in r  
  119.     return self.format('D, j M Y H:i:s O')  
  120.   File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 37, in format  
  121.     pieces.append(force_text(getattr(self, piece)()))  
  122.   File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py", line 78, in force_text  
  123.     s = six.text_type(s)  
  124.   File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 114, in __text_cast  
  125.     return func(*self.__args, **self.__kw)  
  126.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 85, in ugettext  
  127.     return _trans.ugettext(message)  
  128.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 321, in ugettext  
  129.     return do_translate(message, 'ugettext')  
  130.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 298, in do_translate  
  131.     _default = _default or translation(settings.LANGUAGE_CODE)  
  132.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 205, in translation  
  133.     _translations[language] = DjangoTranslation(language)  
  134.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 118, in __init__  
  135.     raise IOError("No translation files found for default language %s." % settings.LANGUAGE_CODE)  
  136. IOError: No translation files found for default language zh-CN.  
  137. [pid: 3318|app: 0|req: 1/1] 219.237.242.144 () {44 vars in 5238 bytes} [Fri May  6 10:19:53 2016] GET / => generated 0 bytes in 243 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)  
  138. Error opening file for reading: Permission denied  
  139. [pid: 3321|app: 0|req: 1/2] 68.180.229.29 () {32 vars in 469 bytes} [Fri May  6 10:21:04 2016] GET /ge/qqt-a653480/ => generated 19272 bytes in 272 msecs (HTTP/1.1 200) 3 headers in 195 bytes (1 switches on core 0)  
  140. Error opening file for reading: Permission denied  
  141. ERROR Internal Server Error: /  
  142. Traceback (most recent call last):  
  143.   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 174, in get_response  
  144.     response = self.process_exception_by_middleware(e, request)  
  145.   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 172, in get_response  
  146.     response = response.render()  
  147.   File "/usr/local/lib/python2.7/dist-packages/django/template/response.py", line 160, in render  
  148.     self.content = self.rendered_content  
  149.   File "/usr/local/lib/python2.7/dist-packages/django/template/response.py", line 137, in rendered_content  
  150.     content = template.render(context, self._request)  
  151.   File "/usr/local/lib/python2.7/dist-packages/django/template/backends/django.py", line 95, in render  
  152.     return self.template.render(context)  
  153.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 206, in render  
  154.     return self._render(context)  
  155.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 197, in _render  
  156.     return self.nodelist.render(context)  
  157.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 992, in render  
  158.     bit = node.render_annotated(context)  
  159.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 959, in render_annotated  
  160.     return self.render(context)  
  161.   File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 173, in render  
  162.     return compiled_parent._render(context)  
  163.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 197, in _render  
  164.     return self.nodelist.render(context)  
  165.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 992, in render  
  166.     bit = node.render_annotated(context)  
  167.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 959, in render_annotated  
  168.     return self.render(context)  
  169.   File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py", line 69, in render  
  170.     result = block.nodelist.render(context)  
  171.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 992, in render  
  172.     bit = node.render_annotated(context)  
  173.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 959, in render_annotated  
  174.     return self.render(context)  
  175.   File "/usr/local/lib/python2.7/dist-packages/django/template/library.py", line 243, in render  
  176.     return t.render(new_context)  
  177.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 208, in render  
  178.     return self._render(context)  
  179.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 197, in _render  
  180.     return self.nodelist.render(context)  
  181.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 992, in render  
  182.     bit = node.render_annotated(context)  
  183.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 959, in render_annotated  
  184.     return self.render(context)  
  185.   File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py", line 220, in render  
  186.     nodelist.append(node.render_annotated(context))  
  187.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 959, in render_annotated  
  188.     return self.render(context)  
  189.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 1043, in render  
  190.     output = self.filter_expression.resolve(context)  
  191.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 736, in resolve  
  192.     new_obj = func(obj, *arg_vals)  
  193.   File "/usr/local/lib/python2.7/dist-packages/django/template/defaultfilters.py", line 51, in _dec  
  194.     return func(*args, **kwargs)  
  195.   File "/usr/local/lib/python2.7/dist-packages/django/template/defaultfilters.py", line 278, in truncatechars  
  196.     return Truncator(value).chars(length)  
  197.   File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 194, in wrapper  
  198.     return func(*args, **kwargs)  
  199.   File "/usr/local/lib/python2.7/dist-packages/django/utils/text.py", line 103, in chars  
  200.     for char in self.add_truncation_text('', truncate):  
  201.   File "/usr/local/lib/python2.7/dist-packages/django/utils/text.py", line 77, in add_truncation_text  
  202.     '%(truncated_text)s...')  
  203.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 93, in pgettext  
  204.     return _trans.pgettext(context, message)  
  205.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 326, in pgettext  
  206.     result = ugettext(msg_with_ctxt)  
  207.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 321, in ugettext  
  208.     return do_translate(message, 'ugettext')  
  209.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 298, in do_translate  
  210.     _default = _default or translation(settings.LANGUAGE_CODE)  
  211.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 205, in translation  
  212.     _translations[language] = DjangoTranslation(language)  
  213.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 118, in __init__  
  214.     raise IOError("No translation files found for default language %s." % settings.LANGUAGE_CODE)  
  215. IOError: No translation files found for default language zh-CN.  
  216. Traceback (most recent call last):  
  217.   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 177, in __call__  
  218.     response = self.get_response(request)  
  219.   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 230, in get_response  
  220.     response = self.handle_uncaught_exception(request, resolver, sys.exc_info())  
  221.   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 284, in handle_uncaught_exception  
  222.     'request': request  
  223.   File "/usr/lib/python2.7/logging/__init__.py", line 1166, in error  
  224.     self._log(ERROR, msg, args, **kwargs)  
  225.   File "/usr/lib/python2.7/logging/__init__.py", line 1258, in _log  
  226.     self.handle(record)  
  227.   File "/usr/lib/python2.7/logging/__init__.py", line 1268, in handle  
  228.     self.callHandlers(record)  
  229.   File "/usr/lib/python2.7/logging/__init__.py", line 1308, in callHandlers  
  230.     hdlr.handle(record)  
  231.   File "/usr/lib/python2.7/logging/__init__.py", line 748, in handle  
  232.     self.emit(record)  
  233.   File "/usr/local/lib/python2.7/dist-packages/django/utils/log.py", line 115, in emit  
  234.     message = "%s %s" % (self.format(no_exc_record), reporter.get_traceback_text())  
  235.   File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 323, in get_traceback_text  
  236.     return t.render(c)  
  237.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 206, in render  
  238.     return self._render(context)  
  239.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 197, in _render  
  240.     return self.nodelist.render(context)  
  241.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 992, in render  
  242.     bit = node.render_annotated(context)  
  243.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 959, in render_annotated  
  244.     return self.render(context)  
  245.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 1043, in render  
  246.     output = self.filter_expression.resolve(context)  
  247.   File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 736, in resolve  
  248.     new_obj = func(obj, *arg_vals)  
  249.   File "/usr/local/lib/python2.7/dist-packages/django/template/defaultfilters.py", line 778, in date  
  250.     return formats.date_format(value, arg)  
  251.   File "/usr/local/lib/python2.7/dist-packages/django/utils/formats.py", line 151, in date_format  
  252.     return dateformat.format(value, get_format(format or 'DATE_FORMAT', use_l10n=use_l10n))  
  253.   File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 367, in format  
  254.     return df.format(format_string)  
  255.   File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 37, in format  
  256.     pieces.append(force_text(getattr(self, piece)()))  
  257.   File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 292, in r  
  258.     return self.format('D, j M Y H:i:s O')  
  259.   File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 37, in format  
  260.     pieces.append(force_text(getattr(self, piece)()))  
  261.   File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py", line 78, in force_text  
  262.     s = six.text_type(s)  
  263.   File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 114, in __text_cast  
  264.     return func(*self.__args, **self.__kw)  
  265.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 85, in ugettext  
  266.     return _trans.ugettext(message)  
  267.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 321, in ugettext  
  268.     return do_translate(message, 'ugettext')  
  269.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 298, in do_translate  
  270.     _default = _default or translation(settings.LANGUAGE_CODE)  
  271.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 205, in translation  
  272.     _translations[language] = DjangoTranslation(language)  
  273.   File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 118, in __init__  
  274.     raise IOError("No translation files found for default language %s." % settings.LANGUAGE_CODE)  
  275. IOError: No translation files found for default language zh-CN.  

经确认是新版本的django包版本中只有zh_Hans目录,没有zh_CN,把zh_Hans目录复制一个zh_CN就Ok了

一楼评论的方法也不错,在settings里面直接改成zh-Hans,这样就不用升级完Django,还去改目录了。

原文地址:https://www.cnblogs.com/dhcn/p/7124684.html