https://narito.ninja/blog/detail/13/
のサイトを参考に半月毎のカレンダーを作りたいです。
基本的な部分は変更しておらず、日にちを返す部分のみ変更を加えました。
この状態だと下記のエラーが出ます。
unsupported operand type(s) for +: 'NoneType' and 'int'
month = month + 1を消した場合以下のエラーが出ます。
'<=' not supported between instances of 'int' and 'NoneType'
整数が帰ってきていないというのは分かるのですが解決方法が分かりません。
urls.py views.pyは上記サイトのまま使用しています。弄ったのはmixins.pyのget_week_daysの部分のみです。
かなり初心者です、よろしくお願いします。
class WeekCalendarMixin(BaseCalendarMixin): """週間カレンダーの機能を提供するMixin""" def get_week_days(self): """その週の日を全て返す""" month = self.kwargs.get('month') year = self.kwargs.get('year') day = self.kwargs.get('day') if month and year and day: date = datetime.date(year=int(year), month=int(month), day=int(day)) if date.day < 21 and day > 4: date = datetime.date(year = int(year),month=int(month+1),day = int(1)) dtlist = [date + datetime.timedelta(days =day) for day in range(0,15)] return dtlist if date.day < 6: dtm = calendar.monthrange(year,month)[1] date = datetime.date(year = int(year),month=int(month),day = int(15)) dtlist = [date + datetime.timedelta(days =day) for day in range(1,dtm-14)] return dtlist if date.day > 20: month =month-1 dtm = calendar.monthrange(year,month)[1] date = datetime.date(year = int(year),month=int(month),day = int(15)) dtlist = [date + datetime.timedelta(days =day) for day in range(1,dtm-14)] return dtlist else: date = datetime.date.today() if date.day < 21 and day > 4: date = datetime.date(year = int(year),month=int(month+1),day = int(1)) dtlist = [date + datetime.timedelta(days =day) for day in range(0,15)] return dtlist if date.day < 6: dtm = calendar.monthrange(year,month)[1] date = datetime.date(year = int(year),month=int(month),day = int(15)) dtlist = [date + datetime.timedelta(days =day) for day in range(1,dtm-14)] return dtlist if date.day > 20: # month = month + 1 dtm = calendar.monthrange(year,month)[1] date = datetime.date(year = int(year),month=int(month),day = int(15)) dtlist = [date + datetime.timedelta(days =day) for day in range(1,dtm-14)] return dtlist def get_week_calendar(self): """週間カレンダー情報の入った辞書を返す""" self.setup_calendar() days = self.get_week_days() first = days[0] last = days[-1] calendar_data = { 'now': datetime.date.today(), 'week_days': days, 'week_previous': first - datetime.timedelta(days=7), 'week_next': first + datetime.timedelta(days=7), 'week_names': self.get_week_names(), 'week_first': first, 'week_last': last, } return calendar_data
回答2件
あなたの回答
tips
プレビュー