シフト管理のアプリを作成しておりますが、初回はすんなりと保存でまでたどり着けるのですが、2回目以降vaildを通すことが出来ません。
特にエラーは出ていません。
POSTを飛ばしても反応せず、データ削除した状態だとvaildを通り次のページまで飛ぶことは出来ています。
モデルではuserとの紐づけと開始時間、終了時間、dateは予定の日
として設定しています。開始、終了はchoice
models.py
1 2SUB_START = ( 3 ('0', '✕'), 4 ('1', '14'), 5 ('2', '16'), 6 ('3', '17'), 7 ('4', '17.5'), 8 ('5', '18'), 9 ('6', '18.5'), 10 ('7', '19'), 11 ('8', '19.5'), 12 ('9', '20'), 13 ('10', '〇'), 14) 15SUB_END = ( 16 ('0', '✕'), 17 ('1', '22'), 18 ('2', '23'), 19 ('3', '〇'), 20 21) 22 23class Schedule(models.Model): 24 """スケジュール""" 25 user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='ユーザー', on_delete=models.SET_NULL, blank=True, null=True) 26 start_time = models.CharField('開始時間', choices= SUB_START, max_length=50,blank=True,default="0") 27 end_time = models.CharField('終了時間', choices= SUB_END, max_length=50,blank=True,default="0") 28 date = models.DateField('日付') 29 # created_at = models.DateTimeField('作成日', default=timezone.now) 30 31 def __int__(self): 32 return self.date 33 34
フォームは、dateの入力のみ非表示に
forms.py class SimpleScheduleForm(forms.ModelForm): """シンプルなスケジュール登録用フォーム""" class Meta: model = Schedule fields = ('start_time', 'end_time', 'date') widgets = { 'date': forms.HiddenInput, }
1日-15日、16日-31日
のようなカレンダーを作ってるのがmonthclarendarmixinです。
多分ここまでは問題ないはず、、、、です。
mixins.py class MonthCalendarMixin(BaseCalendarMixin): """月間カレンダーの機能を提供するMixin""" def get_previous_week(self, date): if date.month == 1 and date.day ==16: return date.replace(year=date.year-1, month=12,day=16) if date.month == 1 and date.day ==1: return date.replace(year=date.year-1, month=12,day=1) if date.day == 16: return date.replace(month =date.month -1 ,day = 16) if date.day == 1: return date.replace(month =date.month -1 ,day = 1) 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.month ==12 and date.day >10: month = 1 year = date.year +1 dtm = calendar.monthrange(year,month)[1] date = datetime.date(year = int(year),month=int(month),day = int(1)) dtlist = [date + datetime.timedelta(days =day) for day in range(0,15)] return dtlist if date.month ==1 and date.day ==1: month =12 year = date.year-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 if date.month != 12 and date.day < 21 and date.day > 4: dtm = calendar.monthrange(year,month)[1] 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() year =int(date.year) month=int(date.month) day=int(date.day) if date.day < 21 and date.day > 5: 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 if date.month ==12 and date.day < 21 and date.day > 5: month = 1 year = date.year +1 dtm = calendar.monthrange(year,month)[1] date = datetime.date(year = int(year),month=int(month),day = int(1)) dtlist = [date + datetime.timedelta(days =day) for day in range(0,15)] return dtlist def name(self): weekname=['月','火','水','木','金','土','日'] name=[] week=[] day = self.get_week_days() for l in day: name.append(l.weekday()) for nn in name: week.append(weekname[nn]) return week def get_month_calendar(self): self.setup_calendar() days = self.get_week_days() first = days[0] last = days[-1] ago = self.get_previous_week(first) calendar_data = { 'now': datetime.date.today(), 'month_days': days, 'month_previous': ago, 'month_next': first, 'week_names': self.name(), } return calendar_data class MonthWithFormsMixin(MonthCalendarMixin): def get_month_forms(self, start, end, days): lookup = { '{}__range'.format(self.date_field): (start, end), 'user__pk': self.kwargs.get('user_pk'), } queryset = self.model.objects.filter(**lookup) days_count = len(days) FormClass = forms.modelformset_factory(self.model, self.form_class, extra=days_count) if self.request.method == 'POST': formset = self.month_formset = FormClass(self.request.POST or None) else: formset = self.month_formset = FormClass(queryset=queryset) day_forms = {day: [] for day in days } dates =[] for bound_form in formset.initial_forms: instance = bound_form.instance date = getattr(instance, self.date_field) dates.append(date) for empty_form, (d, empty_list) in zip(formset.extra_forms, day_forms.items()): if d in dates: pass else: date=d empty_form.initial = {self.date_field: date} empty_list.append(empty_form) for bound_form in formset.initial_forms: instance = bound_form.instance date = getattr(instance, self.date_field) day_forms[date].append(bound_form) return [{key: day_forms[key] for key in itertools.islice(day_forms, 0, days_count)}] def get_month_calendar(self): calendar_context = super().get_month_calendar() month_days = calendar_context['month_days'] month_first = month_days[0] month_last = month_days[-1] calendar_context['month_day_forms'] = self.get_month_forms( month_first, month_last, month_days ) calendar_context['month_formset'] = self.month_formset return calendar_context
views.py class MonthWithFormsCalendar(mixins.MonthWithFormsMixin, generic.View): template_name = 'app/month_with_forms.html' model = Schedule date_field = 'date' form_class = SimpleScheduleForm def get(self, request, **kwargs): print(1) context = self.get_month_calendar() context['user'] = get_object_or_404(User, pk=self.kwargs['user_pk']) return render(request, self.template_name, context) def post(self, request, **kwargs): print(2) context = self.get_month_calendar() user_pk = self.kwargs['user_pk'] user = get_object_or_404(User, pk=user_pk) context['user'] = user formset = context['month_formset'] print(formset) if formset.is_valid(): print(1) instances = formset.save(commit=False) for schedule in instances: schedule.user = user schedule.save() return redirect('app:week_with_schedule', user_pk=user_pk) return render(request, self.template_name, context)
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。