I am using the Django Form Wizard and I have successfully created a multistep form that is able to create a new User. I have a second model called UserAddon that I set a OnetoOne relation to. I cant figure out how to POST to that model and to create that relation in the UserAddon. Also if someone knows, should I be using the checks like
if form.is_valid():
or
if request.method == 'POST':
Within this before I post?
Thanks!
Wizard
class ContactWizard(SessionWizardView):
template_name = "wizardApp/contact_form.html"
def done(self, form_list, **kwargs):
process_form_data(form_list)
return HttpResponseRedirect('../home')
Process form data
def process_form_data(form_list):
form_data = [form.cleaned_data for form in form_list]
first_name = form_data[0]['first_name']
last_name = form_data[0]['last_name']
email = form_data[0]['email']
print(last_name)
print(first_name)
print(email)
user = User.objects.create_user(email)
user.first_name = first_name
user.last_name = last_name
user.email = email
user.save()
##### SOMETHING LIKE THIS ####
user_addon = UserAddon.objects.create(user.email)
return form_data
Its also worth noting my code is written to set the users email address as the username
I was actually able to figure it out see comment
Process form data
def process_form_data(form_list):
form_data = [form.cleaned_data for form in form_list]
first_name = form_data[0]['first_name']
last_name = form_data[0]['last_name']
email = form_data[0]['email']
fav_food = form_data[1]['fav_food']
fav_drink = form_data[1]['fav_drink']
# print(username)
print(last_name)
print(first_name)
print(email)
print(fav_food)
print(fav_drink)
user = User.objects.create_user(email)
user.first_name = first_name
user.last_name = last_name
user.email = email
user.save()
#######Solution HERE##########
user_addon = UserAddon.objects.create(user=user,fav_food=fav_food,fav_drink=fav_drink)
user_addon.save()
I am still unclear on checking request for post and if the form is valid so any help there is still apreciated!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.