更新:研究了一下這個問題後,(ASCII解決錯誤,但現在返回ValueError異常),我發現,這是一個記錄的缺陷(issue #3067),因此無法解決,直到錯誤解決在Python 2.7.3不能在Django本地化蟒蛇HTMLCalendar
我試圖定位在Django和Python HTMLCalendar做了一個日曆,沒有結果。日曆是用code from Elving Uggedal製作的,但似乎無法對其進行本地化。
下面是代碼:
models.py
import calendar
from datetime import date
from itertools import groupby
from django.utils.html import conditional_escape as esc
class EventCalendar(calendar.LocaleHTMLCalendar):
"""
Event calendar is a basic calendar made with HTMLCalendar module.
"""
def __init__(self, events, *args, **kwargs):
self.events = self.group_by_day(events)
super(EventCalendar, self).__init__(*args, **kwargs)
def formatday(self, day, weekday):
if day != 0:
cssclass = self.cssclasses[weekday]
if date.today() == date(self.year, self.month, day):
cssclass += ' today'
if day in self.events:
cssclass += ' filled'
body = ['<ul>']
for event in self.events[day]:
body.append('<li>')
body.append('<a href="%s">' % event.get_absolute_url())
body.append(esc(event.title))
body.append('</a></li>')
body.append('<ul>')
return self.day_cell(cssclass, '%d %s' % (day, ''.join(body)))
return self.day_cell(cssclass, day)
return self.day_cell('noday', ' ')
def formatmonth(self, year, month):
self.year, self.month = year, month
return super(EventCalendar, self).formatmonth(year, month)
def group_by_day(self, events):
field = lambda event: event.meeting_date.day
return dict(
[(day, list(items)) for day, items in groupby(events, field)]
)
def day_cell(self, cssclass, body):
return '<td class="%s">%s</td>' % (cssclass, body)
views.py
from django.shortcuts import render_to_response, get_object_or_404
from django.utils.safestring import mark_safe
from django.template import RequestContext
from django.utils import translation
from e_cidadania.apps.spaces.models import Meeting, Space
from e_cidadania.apps.cal.models import EventCalendar
def calendar(request, space_name, year, month):
# Avoid people writing wrong numbers or any program errors.
if int(month) not in range(1, 13):
return render_to_response('cal/error.html',
context_instance=RequestContext(request))
place = get_object_or_404(Space, url=space_name)
next_month = int(month) + 1
prev_month = int(month) - 1
meetings = Meeting.objects.order_by('meeting_date') \
.filter(space = place,
meeting_date__year = year,
meeting_date__month = month)
cur_lang = translation.get_language()
print 'DEBUG:LANG: %s' % cur_lang
cur_locale = translation.to_locale(cur_lang)+'.UTF-8' #default encoding with django
print 'DEBUG:LOCALE: %s' % cur_locale
cal = EventCalendar(meetings, settings.FIRST_WEEK_DAY, cur_locale).formatmonth(int(year), int(month))
return render_to_response('cal/calendar.html',
{'calendar': mark_safe(cal),
'nextmonth': '%02d' % next_month,
'prevmonth': '%02d' % prev_month,
'get_place': place},
context_instance = RequestContext(request))
我使用LocalizeHTMLCalendar嘗試,它應該返回一個本地化的HTMLCalendar ,但使用它作爲基類時,它將在日曆mo中返回一個ASCII解碼錯誤dule(文件中聲明瞭utf-8編碼)。
我也嘗試覆蓋語言環境數組day_name,day_abbr,month_name和month_abbr,這在日曆渲染結束時爲空。
例子:
from django.utils.translation import ugettext_lazy as _
calendar.day_name = [_('Monday'), _('Tuesday'), _('Wednesday'), _('Thursday'), _('Friday'), _('Saturday'), _('Sunday'), ]
我似乎無法找到一種方法來定位這個日曆。有任何想法嗎?
不錯!但是,就我所見,該代碼僅適用於一種語言。我想我可以通過get_current_language()獲取當前用戶lang,因爲我的應用程序是多語言的。無論如何,感謝您的答案,我要做一些測試 –
是的,而不是從設置獲取區域設置名稱,從用戶配置文件中獲取它,並將其傳遞給構造函數。應該工作正常。 –
我修改了代碼,但現在它返回一個ValueError「太多值解包」,就像它無法管理區域設置:/ –