def form_valid(self, form): context = self.get_context_data() address_form = AddressForm(self.request.POST) salary_form = SalaryForm(self.request.POST) teacher_form = TeacherForm(self.request.POST, self.request.FILES) if form.is_valid() and address_form.is_valid( ) and teacher_form.is_valid(): form.instance.password = str(uuid4()) form.instance.type_of_user = USER_TYPE_CHOICE[1][0] form.save() address_form.instance.user = form.instance address_form.save() teacher_form.instance.personal_details = form.instance teacher_form.save() salary_form.instance.teacher_id = teacher_form.instance salary_form.save() return super().form_valid(form) else: return render(self.request, template_name=self.template_name, context={ 'address_form': address_form, 'teacher_form': teacher_form, 'form': form })
def form_valid(self, form): context = self.get_context_data() address_form = AddressForm( self.request.POST, instance=self.object.address_set.all().first()) salary_form = SalaryForm(self.request.POST, instance=self.object.teacher.salary) teacher_form = TeacherForm(self.request.POST, self.request.FILES, instance=self.object.teacher) if form.is_valid() and address_form.is_valid( ) and teacher_form.is_valid(): form.save() address_form.save() teacher_form.save() salary_form.save() return super().form_valid(form) else: return render(self.request, template_name=self.template_name, context={ 'address_form': address_form, 'teacher_form': teacher_form, 'form': form })
def updatet(request, id): teacher = Teacher.objects.get(id=id) form = TeacherForm(request.POST, instance=teacher) if form.is_valid(): form.save() return redirect('../showt') return render(request, "editt.html", {'teacher': teacher})
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['address_form'] = AddressForm() context['teacher_form'] = TeacherForm() context['salary_form'] = SalaryForm() return context
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['address_form'] = AddressForm( instance=self.object.address_set.all().first()) context['teacher_form'] = TeacherForm(instance=self.object.teacher) context['salary_form'] = SalaryForm( instance=self.object.teacher.salary) return context
def index(request): teacher = Teacher.objects.all() if request.method == "POST": form = TeacherForm(request.POST) if form.is_valid(): try: form.save() return redirect('../../teacher/showt') except: pass else: form = TeacherForm() return render(request, "indext.html", {'form': form})
def test_teacher_form_rejects_invalid_data(self): form = TeacherForm(self.bad_data) self.assertFalse(form.is_valid())
def test_teacher_form_accepts_valid_data(self): form = TeacherForm(self.data) self.assertTrue(form.is_valid())