def add_section(request): if request.method == 'POST': raw_data = SectionForm(request.POST) if raw_data.is_valid(): data = raw_data.cleaned_data Sections.objects.create(**data) return redirect(view_sections) context = {'section_form': raw_data} return render(request, 'homework6_add_section.html', context) else: sections = Sections.objects.filter() context = {'section_form': SectionForm(), 'sections': sections} return render(request, 'homework6_add_section.html', context)
def post(self, request, section_id=None): section = Section() if section_id: section = Section.objects.get(pk=section_id) form = SectionForm(request.POST, request.FILES, instance=section) if form.is_valid(): form.save() return redirect("inventory_section") else: return self.render_to_response({ "form": form, })