V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
foonpcf
V2EX  ›  Python

Python calendar 問題.

  •  
  •   foonpcf · 2014-05-16 10:34:21 +08:00 · 3201 次点击
    这是一个创建于 3845 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我參考了以下網站做了行事曆,但是沒有下一個月及上一個月功能,請問怎樣增加這個功能呢?

    < http://journal.uggedal.com/creating-a-flexible-monthly-calendar-in-django>;
    2 条回复    2014-05-19 22:18:59 +08:00
    MasterYoda
        1
    MasterYoda  
       2014-05-16 16:34:33 +08:00
    公司电脑没翻墙。 你给的链接上不去啊。
    胡乱猜测下你的意思啊:
    下一个月上一个月是不是url最后参数不同。这样和分页一样吧。
    foonpcf
        2
    foonpcf  
    OP
       2014-05-19 22:18:59 +08:00
    @MasterYoda 試許我貼一貼整段代碼

    from __future__ import with_statement
    from cms.plugin_base import CMSPluginBase
    from cms.plugin_pool import plugin_pool
    from models import Event
    from django.utils.translation import ugettext as _
    from django.utils.safestring import mark_safe
    from django.core.urlresolvers import reverse

    from calendar import HTMLCalendar, TimeEncoding, month_name, day_abbr

    from django.utils.dates import MONTHS
    from django.utils.dates import WEEKDAYS_ABBR

    from datetime import date
    from itertools import groupby

    from django.utils.html import conditional_escape as esc

    import locale

    class EventCalendar(HTMLCalendar):
    """http://journal.uggedal.com/creating-a-flexible-monthly-calendar-in-django"""
    def __init__(self, events):
    super(EventCalendar, self).__init__()
    self.events = self.group_by_day(events)

    def formatday(self, day, weekday):
    if day != 0:
    cssclass = self.cssclasses[weekday]
    if date.today() == date(self.year, self.month, day):
    cssclass += ' today'
    body = '<a class="tipsy" href="#" title="%s">%d</a>' % (_('Today'), day)
    if day in self.events:
    d = reverse('events_day', args=[self.year, self.month, day])
    body = '<a class="tipsy" href="%s" title="%s">%d</a>' % (d, _("Events found"), day)
    return self.day_cell(cssclass, body)

    if day in self.events:
    cssclass += ' filled'
    title = ['<ul class=event>']
    for event in self.events[day]:
    title.append('<li><b>%s</b></li>' % esc(event.name))
    title.append('</ul>')
    d = reverse('events_day', args=(self.year, self.month, day))
    body = '<a class="tipsy" href="%s" title="%s">%d</a>' % (d, _("Events found"), day)
    return self.day_cell(cssclass, body)
    return self.day_cell(cssclass, day)
    return self.day_cell('noday', '&nbsp;')

    def formatweekday(self, day):
    return '<th class="%s">%s</th>' % (self.cssclasses[day], WEEKDAYS_ABBR[day].title())

    def formatmonthname(self, theyear, themonth, withyear=True):
    if withyear:
    s = '%s %s' % (MONTHS[themonth].title(), theyear)
    else:
    s = '%s' % MONTHS[themonth].title()
    return '<tr><th colspan="7" class="month">%s</th></tr>' % s

    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.start.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)


    class CMSCalendarEventsPlugin(CMSPluginBase):

    name = _("Calendar Events")
    render_template = "simple_events/events_calendar.html"

    def render(self, context, instance, placeholder):
    today = date.today()
    year = today.year
    month = today.month

    request = context['request']
    if request.GET:
    month = int(request.GET.get('month') or month)
    year = int(request.GET.get('year') or year)
    first_day = date(year,month,1)
    if month == 12:
    last_day = date(year+1,1,1)
    else:
    last_day = date(year,month+1,1)
    events = Event.objects.filter(start__gte=first_day).filter(start__lt=last_day)
    cal = EventCalendar(events).formatmonth(year, month)
    context.update({'instance':instance,
    'events': events,
    'calendar': mark_safe(cal),
    'placeholder':placeholder})
    return context

    plugin_pool.register_plugin(CMSCalendarEventsPlugin)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   913 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 20:02 · PVG 04:02 · LAX 12:02 · JFK 15:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.