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 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 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 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})