def register(request): context = RequestContext(request) # Boolean value for telling the template whether the registration was successful # Initially set to False. Changes to True when registration succeeds. registered = False # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. user_form = UserForm(data=request.POST) profile_form = userProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.is_active = False user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user # Did the user provide a profile picture? # If so, we need to get it from the imput form and put it in the userProfile model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the userProfile model instance profile.save() # update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something # Print problems to the terminal and show them to user. else: print user_form.errors, profile_form.errors # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank waiting for user input. else: user_form = UserForm() profile_form = userProfileForm() # Render the template depending on the context. return render_to_response('userAuth/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)
def register(request): context = RequestContext(request) # Boolean value for telling the template whether the registration was successful # Initially set to False. Changes to True when registration succeeds. registered = False # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. user_form = UserForm(data=request.POST) if user_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.is_active = False user.save() # Now sort out the UserProfile instance. create_profile(user) # update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something # Print problems to the terminal and show them to user. else: print user_form.errors # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank waiting for user input. else: user_form = UserForm() # Render the template depending on the context. return render_to_response('userAuth/register.html', {'user_form': user_form, 'registered': registered}, context)