def register_page(request): #2, we create the instance of the form class with the request method register_form = RegisterForm(request.POST or None) #3, we create the context #4, we check if the form is valid if register_form.is_valid(): #5 we get the cleaned datas username = register_form.cleaned_data.get('username') email = register_form.cleaned_data.get('email') password = register_form.cleaned_data.get('password') #6 we use the auth.user model to create the new_user new_user = User.objects.create_user(username=username, email=email, password=password) #7 we get the other data from the form fullname = register_form.cleaned_data.get('fullname') occupation = register_form.cleaned_data.get('occupation') address = register_form.cleaned_data.get('address') date_of_birth = register_form.cleaned_data.get('date_of_birth') #8 we store the data into the User Profile model, while inheriting for the auth.user new_user1 = UserProfile(user=new_user, fullname=fullname, occupation=occupation, address=address, date_of_birth=date_of_birth) #9 we save the updated user new_user1.save() #10 we check to make sure user exist if new_user1 is not None: #11 if user exist log in user login(request, new_user) #12 redirect user to home page return redirect('/') return render(request, 'register_page.html', {'register_form': register_form})
def create_userprofile_from_cdata(username, email, password, role): # returns user, not userprofile user = User.objects.create_user(username, email, password) profile = UserProfile(user=user, role=role) profile.save() return user