From c25d409a11e43b1eb6ee5545032f91c6dabf3c6f Mon Sep 17 00:00:00 2001 From: Benni Baermann Date: Mon, 5 Oct 2020 13:35:28 +0200 Subject: [PATCH] gather data and write to database in multistep form --- input/views.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/input/views.py b/input/views.py index cafaa9b..e6a2ee1 100644 --- a/input/views.py +++ b/input/views.py @@ -1,7 +1,7 @@ from django.shortcuts import render from django.forms import modelformset_factory from django.http import HttpResponse -from formtools.wizard.views import SessionWizardView +from formtools.wizard.views import CookieWizardView from .forms import ProjectForm, VolunteerForm, LibraryForm from .models import Project @@ -30,10 +30,25 @@ def done(request): def extern(request): return HttpResponse("The world out there is large and dangerous") -class ExternView(SessionWizardView): +class ExternView(CookieWizardView): template_name = "input/extern.html" form_list = [VolunteerForm, LibraryForm] + def done(self, form_list, **kwargs): + print('ExternView.done() reached') + # gather data from all forms + data = {} + for form in form_list: + data = {**data, **form.cleaned_data} + print(data) + # write data to database + form = form.save(commit=False) + # this is ugly code. how can we copy this without explicit writing? + form.realname = data['realname'] + form.username = data['username'] + form.email = data['email'] + form.save() + return done(self.request) # return render(self.request, 'saved', { # 'form_data': [form.cleaned_data for form in form_list],