Hi Everyone,
I have created a my own custom user model:
and my registering forms are:
class RegistrationForm(UserCreationForm):
class Meta:
model = Account
fields = ("username", "password1", "password2")
class RegistrationForm2(UserCreationForm):
guardianSource = forms.BooleanField(initial=True, required=False)
bbcSource = forms.BooleanField(initial=False, required=False)
independentSource = forms.BooleanField(initial=False, required=False)
categoryCoronaVirus = forms.BooleanField(initial=True, required=False)
categoryPolitics = forms.BooleanField(initial=False, required=False)
categorySport = forms.BooleanField(initial=False, required=False)
class Meta:
model = Account
fields = ("guardianSource", "bbcSource", "independentSource", "categoryCoronaVirus", "categoryPolitics", "categorySport")
On registering a user I have 2 views, first it renders a form with the username and password, and on submitting it registers the user and redirects to another form with 6 booleanFields that I want to save into the database for the registered user:
def registration(request):
context = {}
if request.POST:
form = RegistrationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
raw_password = form.cleaned_data.get('password1')
account = authenticate(username=username, password=raw_password)
login(request, account)
return redirect('register2') #redirect to 2nd form
else:
context['registration_form'] = form
else: # GET request
form = RegistrationForm()
context['registration_form'] = form
return render(request, 'accounts/register.html', context)
def registration2(request):
context = {}
if request.POST:
form = RegistrationForm2(request.POST)
if form.is_valid():
form.save()
return redirect('home')
else:
context['registration_form2'] = form
else:
form = RegistrationForm()
context['registration_form2'] = form
return render(request, 'accounts/register-config.html', context)
The problem is that on registering the second form, the boolean types are not saved into the users profile inside the database and it does not redirects to the home page.
Thanks in advance for your help.