def edit_profile(request): user = request.user user_profile = user.user_profile error = False msg = None if request.POST: user_form = UserEditForm(request.POST, instance=user) profile_form = UserProfileForm(request.POST, instance=user_profile) if user_form.is_valid(): user_profile = user_form.save(commit=False) user_profile.save() profile_form.is_valid() print profile_form if profile_form.is_valid(): profile = profile_form.save(commit=False) if "url" in request.POST: profile.picture = cropAndSave(user, request.POST) # return here if different behaviour desired profile.save() return JsonResponse({error: False,msg: "Profile Updated!"}) else: return JsonResponse({error: True,msg: "Please check the required fields"}) else: return JsonResponse({error: True,msg: "Please check the required fields"}) else: user_form = UserEditForm(instance=user) profile_form = UserProfileForm(instance=user_profile) return render(request, "mentee/edit_profile.html", locals())
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. data = request.POST.copy() # so we can manipulate data # random username data['username'] = data[ 'email'] #useless,rather keep email as data['email'] username ''.join([choice(letters) for i in xrange(30)]) user_form = UserForm(data) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_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. #MISTAKE-was rehashing the hashed password,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. profile = profile_form.save(commit=False) profile.user = user cropAndSave(user, request.POST) if "url" in request.POST: profile.picture = request.POST['url'] else: HttpResponse("Image URL missing :(") profile.is_new = False profile.is_mentor = False profile.save() # Update our variable to tell the template registration was successful. registered = True return HttpResponseRedirect("/user/thank-you/") # 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, profile_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() profile_form = UserProfileForm() # Render the template depending on the context. return render_to_response( 'mentee/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)