def user_profile(request): user = User.objects.get_or_create(pk=request.user.id) if request.method == 'POST': form = UserProfileForm(request.POST, instance=request.user) if form.is_valid(): form.save() form.user = request.user return HttpResponseRedirect('/accounts/loggedin') else: form= UserProfileForm(instance=request.user) args={} args.update(csrf(request)) args['form'] = form return render_to_response('profile.html',args, context_instance=RequestContext(request)) # # # def user_profile(request): # success = False # user = User.objects.get(pk=request.user.id) # if request.method == 'POST': # upform = UserProfileForm(request.POST, instance=user.get_profile()) # if upform.is_valid(): # up = upform.save(commit=False) # up.user = request.user # up.save() # success = True # else: # upform = UserProfileForm(instance=request.user) # # return render_to_response('profile.html', # locals(), context_instance=RequestContext(request)) #
def profile(request): if request.method == "POST": if request.user.is_authenticated(): try: u=User.objects.get(username=request.user.username) except User.DoesNotExist: u = None print u up=UserProfile.objects.get_or_create(user=u)[0] print up user_profile_form=UserProfileForm(data=request.POST) user_profile_form.user=u if user_profile_form.is_valid(): print "valid form" if 'avatar' in request.FILES: up.location=request.POST['location'] #up.avatar=request.FILES['avatar'], up.user_skills=request.POST['skills'] up.save() image=request.FILES['avatar'] print image.content_type print image.size if image.content_type in ["image/jpeg","image/png","image/jpg"] and (image.size/1024) <= 1024: up.avatar.save(image.name,image) else: return render_to_response('after_profile_update.html', {"message": "file type is invalid or size exceeds 1 MB"}, RequestContext(request)) else: up.location=request.POST['location'] up.user_skills=request.POST['skills'] up_avatar=up.avatar up.avatar=up_avatar up.save() return render_to_response( 'after_profile_update.html', {"message": "Your profile has been updated"}, RequestContext(request)) else: print "the form submitted was invalid" print user_profile_form.errors #this handles the ValidationError raised in forms.py return render_to_response('after_profile_update.html', {"message": "please enter valid data.The location and skills field are required. Profile photo is optional"}, RequestContext(request)) else: #the user has to login to post and is displayed the login to post message if he does so without logging in return HttpResponse("login to post") # displaying the form for the first time. else: user_profile_form = UserProfileForm() return render_to_response( 'update_profile.html', {'user_profile_form': user_profile_form}, RequestContext(request))
def profile(request): if request.method == "POST": if request.user.is_authenticated(): try: u = User.objects.get(username=request.user.username) except User.DoesNotExist: u = None print u up = UserProfile.objects.get_or_create(user=u)[0] print up user_profile_form = UserProfileForm(data=request.POST) user_profile_form.user = u if user_profile_form.is_valid(): print "valid form" if 'avatar' in request.FILES: up.location = request.POST['location'] # up.avatar=request.FILES['avatar'], up.user_skills = request.POST['skills'] up.save() image = request.FILES['avatar'] print image.content_type print image.size from django.core.files.images import * if image.content_type in [ "image/png" ] and (image.size / 1024) <= 1024: image_dim = get_image_dimensions(image) print image_dim[0] <= 500 print image_dim[1] <= 500 else: return render_to_response( 'user_profile/after_profile_update.html', {"message": "png file required"}, RequestContext(request)) if image.content_type in [ "image/png" ] and (image.size / 1024) <= 1024: if image_dim[0] <= 500 and image_dim[1] <= 500: up.avatar.save(image.name, image) else: return render_to_response( 'user_profile/after_profile_update.html', { "message": "Your pictures resolution should not be more than 500*500" }, RequestContext(request)) else: return render_to_response( 'user_profile/after_profile_update.html', { "message": "file type is invalid or size exceeds 1 MB." }, RequestContext(request)) else: up.location = request.POST['location'] up.user_skills = request.POST['skills'] up_avatar = up.avatar up.avatar = up_avatar up.save() return render_to_response( 'user_profile/after_profile_update.html', {"message": "Your profile has been updated"}, RequestContext(request)) else: print "the form submitted was invalid" print user_profile_form.errors # this handles the ValidationError raised in forms.py return render_to_response( 'user_profile/after_profile_update.html', { "message": "please enter valid data.The location and skills field are required. Profile photo is optional" }, RequestContext(request)) else: # the user has to login to post and is displayed the login to post # message if he does so without logging in return HttpResponse("login to post") # displaying the form for the first time. else: user_profile_form = UserProfileForm() return render_to_response('user_profile/update_profile.html', {'user_profile_form': user_profile_form}, RequestContext(request))