过去一年,我一直在使用Django开发应用程序。该应用程序是在Debian环境中编写的,开发和测试完全在debain机器上完成。
最近,我决定将应用程序移动到我的本地Mac OS X笔记本电脑上。使用port我做了一个虚拟环境并从github中取出了我的项目。当我尝试运行服务器时,我得到“不支持的语言环境设置”异常。
我的应用程序大量使用区域设置,我注意到虽然我的机器中的语言设置是为en_US.UTF-8设置的,而在debian机器上它是en_US.UTF8。我不知道破折号是否是它失败的原因。
我的应用程序使用django-localeurl为i10n和i18n创建语言代码url架构。
以下是我的例外
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/en/
Django Version: 1.3.1
Python Version: 2.7.1
Installed Applications:
['localeurl',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'django.contrib.comments',
'libraries.plugins.piston',
'nani',
'haystack',
'compressor',
'libraries.plugins.social_auth',
'registration',
'djcelery',
'django.contrib.markup',
'south',
'libraries.plugins.storages',
'libraries.plugins.django_messages',
'merlin',
'seacucumber',
'timezones',
'treebeard',
'apps.core',
'apps.core.mediastore',
'apps.core.accounts',
'apps.core.mazvars',
'apps.core.mazguard',
'apps.core.system',
'apps.core.sponsorship',
'apps.portal.default',
'apps.portal.support',
'apps.portal.pages',
'apps.portal.blog',
'apps.portal.developer',
'apps.listing',
'apps.listing.postfix',
'apps.listing.category',
'apps.listing.post',
'apps.listing.home',
'apps.listing.shoutout',
'apps.listing.taggables',
'apps.listing.comment',
'apps.listing.badge',
'apps.listing.geo',
'apps.listing.reputation',
'apps.mzn.shorturl',
'grappelli.dashboard',
'grappelli',
'django.contrib.admin',
'django.contrib.admindocs',
'debug_toolbar']
Installed Middleware:
('localeurl.middleware.LocaleURLMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'apps.core.middleware.FallbackLocaleMiddleware',
'apps.core.accounts.middleware.LoggingMiddleware',
'apps.core.accounts.middleware.CompleteEmail',
'apps.core.accounts.middleware.setref',
'apps.core.accounts.middleware.FollowUser',
'apps.core.accounts.middleware.UnFollowUser',
'apps.core.accounts.middleware.FraudLocked',
'apps.core.middleware.DefaultAdminLocaleMiddleware',
'apps.core.middleware.DefaultSiteMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware')
Traceback:
File "/Users/mo/Projects/pythonic/mazban/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/mo/Projects/pythonic/mazban/mazban/apps/listing/home/views.py" in home
23. cal = render_event_calendar_widget(language_code=request.LANGUAGE_CODE)
File "/Users/mo/Projects/pythonic/mazban/mazban/apps/listing/post/event_views.py" in render_event_calendar_widget
115. return cal.render()
File "/Users/mo/Projects/pythonic/mazban/mazban/apps/core/utils.py" in render
81. [day for day in cal.formatweekheader(self.wh).split(' ')]
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/calendar.py" in formatweekheader
297. return ' '.join(self.formatweekday(i, width) for i in self.iterweekdays())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/calendar.py" in <genexpr>
297. return ' '.join(self.formatweekday(i, width) for i in self.iterweekdays())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/calendar.py" in formatweekday
511. with TimeEncoding(self.locale) as encoding:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/calendar.py" in __enter__
490. _locale.setlocale(_locale.LC_TIME, self.locale)
File "/Users/mo/Projects/pythonic/mazban/lib/python2.7/locale.py" in setlocale
531. return _setlocale(category, locale)
Exception Type: Error at /
Exception Value: unsupported locale setting
任何想法是什么导致了这个?
更新
当我登录终端并写“locale”时,我得到了以下
(mazban)Mohammed-Mughrabis-MacBook-Pro:mazban mo$ locale
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL=
答案 0 :(得分:3)
你的代码在哪里设置它?这对我有用,因为Linux和OSX机器各不相同,我在两者上使用我的代码,我写了这个异常块:
try:
import locale
locale.setlocale(locale.LC_ALL, 'en_US.utf8')
except Exception:
try:
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
except Exception as e:
messages.error(request, 'An error occurred: {0}'.format(e))
答案 1 :(得分:2)
为了将来参考,我通过将LC_ALL导出为'en_US'解决了我的问题来解决了这个问题
export LC_ALL='en_US'