提交两个表格时的表格验证

时间:2019-01-17 07:38:32

标签: django django-models django-forms django-templates django-views

After Submiting Form 提交表格后

Before Submitting Form 提交表格之前

来自表单错误的验证。当我从form_hourget函数中删除post时,则form,但是当我放置form_hour时,就会出现验证错误。基本上,我正在更新名为form的表单并创建form_hour

Views.py

class ClockOutAddView(LoginRequiredMixin, generic.View):
        # model = TimesheetEntry
        template_name = 'admin/clock/clock_form.html'
        # form_class = ClockOutForm
        success_url = '/useradmin/timesheet/'

        def get(self, request, pk, *args, **kwargs):
            form =  ClockOutForm(instance=TimesheetEntry.objects.get(id=pk))
            form_hour = ClockOutHourForm()
            return render(request, self.template_name, {'form': form, 'form_hour':form_hour})


        def post(self, request, pk, *args, **kwargs):
            form = ClockOutForm(request.POST, instance=TimesheetEntry.objects.get(id=pk))
            form_hour = ClockOutHourForm(request.POST)
            print(form.error_class)
            print(form_hour.error_class)
            print('Outside Valid')

            if form.is_valid():
                form.instance.timesheet_is_running =False
                print('Inside Valid')
                # form.save()
                # form.instance.timesheet_hour_created_by = self.request.user
                # form.instance.timesheet_hour_updated_by = self.request.user

                return HttpResponseRedirect(self.success_url)

            return render(request, self.template_name, {'form': form, 'form_hour':form_hour})

Form.py

class ClockInForm(forms.ModelForm):

    class Meta:
        model = TimesheetEntry
        fields = ['timesheet_jobs', 'timesheet_clock_in_date', 'timesheet_clock_in_time']


class ClockOutForm(forms.ModelForm):

    class Meta:
        model = TimesheetEntry
        fields = ['timesheet_jobs', 
        'timesheet_clock_in_date', 
        'timesheet_clock_in_time',
        'timesheet_clock_out_date', 
        'timesheet_clock_out_time',
        'timesheet_note'
        ]

Models.py

class TimesheetEntry(models.Model):
    timesheet_users = models.ForeignKey(User, on_delete=models.CASCADE,related_name='timesheet_users')
    timesheet_jobs = models.ForeignKey(Jobs, on_delete=models.CASCADE,related_name='timesheet_jobs')
    timesheet_clock_in_date = models.DateField()
    timesheet_clock_in_time = models.TimeField()
    timesheet_clock_on = models.DateTimeField(auto_now_add=True)
    timesheet_clock_in_by = models.ForeignKey(User, on_delete=models.CASCADE,related_name='timesheet_user_clock_in_by')
    timesheet_clock_out_date = models.DateField(blank=True, null=True)
    timesheet_clock_out_time = models.TimeField(blank=True, null=True)
    timesheet_clock_out_on = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    timesheet_clock_out_by = models.ForeignKey(User, on_delete=models.CASCADE,related_name='timesheet_user_clock_out_by')
    timesheet_note = models.CharField(max_length=100)
    timesheet_is_edited = models.BooleanField(default=False)
    timesheet_is_out_edited = models.BooleanField(default=False)
    timesheet_is_running = models.BooleanField(default=False)

    class Meta:
        db_table = "timesheet_entry"


class TimesheetHours(models.Model):
    timesheet = models.ForeignKey(TimesheetEntry, on_delete=models.CASCADE,related_name='timesheet_hours')
    timesheet_hour_regular = models.IntegerField(blank=True, null=True)
    timesheet_hour_overtime = models.IntegerField(blank=True, null=True)
    timesheet_hour_doubletime = models.IntegerField(blank=True, null=True)
    timesheet_hour_travel = models.IntegerField(blank=True, null=True)
    timesheet_hour_admin = models.IntegerField(blank=True, null=True)
    timesheet_hour_other = models.IntegerField(blank=True, null=True)

1 个答案:

答案 0 :(得分:0)

您必须验证以下两种形式

def post(self, request, pk, *args, **kwargs):
     form = ClockOutForm(request.POST, instance=TimesheetEntry.objects.get(id=pk))
     form_hour = ClockOutHourForm(request.POST)

     if form.is_valid() and form_hour.is_valid():
         # your business logic
         return HttpResponseRedirect(self.success_url)

     return render(request, self.template_name, {'form': form, 'form_hour':form_hour})