gather data and write to database in multistep form

This commit is contained in:
Benni Baermann 2020-10-05 13:35:28 +02:00
parent 620511359d
commit c25d409a11

View file

@ -1,7 +1,7 @@
from django.shortcuts import render from django.shortcuts import render
from django.forms import modelformset_factory from django.forms import modelformset_factory
from django.http import HttpResponse from django.http import HttpResponse
from formtools.wizard.views import SessionWizardView from formtools.wizard.views import CookieWizardView
from .forms import ProjectForm, VolunteerForm, LibraryForm from .forms import ProjectForm, VolunteerForm, LibraryForm
from .models import Project from .models import Project
@ -30,10 +30,25 @@ def done(request):
def extern(request): def extern(request):
return HttpResponse("The world out there is large and dangerous") return HttpResponse("The world out there is large and dangerous")
class ExternView(SessionWizardView): class ExternView(CookieWizardView):
template_name = "input/extern.html" template_name = "input/extern.html"
form_list = [VolunteerForm, LibraryForm] form_list = [VolunteerForm, LibraryForm]
def done(self, form_list, **kwargs): 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 done(self.request)
# return render(self.request, 'saved', { # return render(self.request, 'saved', {
# 'form_data': [form.cleaned_data for form in form_list], # 'form_data': [form.cleaned_data for form in form_list],