def register(request): # Like before, get the request's context. context = RequestContext(request) # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value 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. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) student_form = StudentForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and student_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) 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. student = student_form.save(commit=False) student.user = user if 'avatar' in request.FILES: student.avatar = request.FILES['avatar'] # Now we save the UserProfile model instance. student.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print(user_form.errors, student_form.errors) # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() student_form = StudentForm() # Render the template depending on the context. return render_to_response( 'rides/register.html', {'user_form': user_form, 'student_form': student_form, 'registered': registered}, context)
def user_register(request): if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) if user_form.is_valid(): user = user_form.save() password = user.password user.set_password(user.password) user.save() u = authenticate(username=user.username, password=password) login(request, u) return HttpResponseRedirect(reverse('rides:eventbrite_permission')) else: user_form = UserForm() return render(request, 'register.html', {'form': user_form})
def register(request): context = RequestContext(request) registered = False if request.method == 'POST': # Checks POST Request user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) # TO set the Hashed Password user.save() profile = profile_form.save( commit=False) #To set the picture and website profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() context_dict = { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered } return render_to_response('rides/register.html', context_dict, context)
def register(request): context = RequestContext(request) registered = False if request.method == "POST": # Checks POST Request user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) # TO set the Hashed Password user.save() profile = profile_form.save(commit=False) # To set the picture and website profile.user = user if "picture" in request.FILES: profile.picture = request.FILES["picture"] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() context_dict = {"user_form": user_form, "profile_form": profile_form, "registered": registered} return render_to_response("rides/register.html", context_dict, context)
def register(request): # Like before, get the request's context. context = RequestContext(request) # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value 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. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) student_form = StudentForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and student_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) 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. student = student_form.save(commit=False) student.user = user if 'avatar' in request.FILES: student.avatar = request.FILES['avatar'] # Now we save the UserProfile model instance. student.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print(user_form.errors, student_form.errors) # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() student_form = StudentForm() # Render the template depending on the context. return render_to_response( 'rides/register.html', { 'user_form': user_form, 'student_form': student_form, 'registered': registered }, context)