def new_log(request): form = StepForm(data=request.POST) # TODO Switch to using form.save instead on model orm # See end of chapter 11 if form.is_valid(): log_ = Log.objects.create() Step.objects.create( count=request.POST['count'], log=log_, date=request.POST['date']) return redirect(log_) else: return render(request, 'home.html', {'form': form})
def view_log(request, log_num): log_ = Log.objects.get(id=log_num) form = StepForm() steps = Step.objects.filter(log=log_) if request.method == 'POST': form = StepForm(data=request.POST) if form.is_valid(): Step.objects.create( count=request.POST['count'], log=log_, date=request.POST['date']) return redirect(log_) return render( request, 'log.html', {'steps': steps, 'log': log_, 'form': form})
def test_form_validation(self): form = StepForm(data={'count': 0}) self.assertFalse(form.is_valid()) self.assertEqual(form.errors['count'], [ZERO_ERROR])