I have a view for an account profile. I'd like the user to be able to edit an image once they've added one. I have the edit code. Once a user edits an image, it adds that image to the db, but doesn't remove the previous image.
So the question is, how do I do the following:
Check to see if an image exists If it does delete it Add the new image
My view is located below, any suggestions are welcome.
@login_required def profile_img_edit(request, username, id): '''Edit a Profile Image''' messages.success(request, "Your changes were saved!") user = get_object_or_404(User, username=username) if request.user != user: return permission_denied(request) profile_img = get_object_or_404(ProfileImage, user=user, id=id) if request.method == 'POST': form = Profile_ImageEditForm(request.POST, request.FILES, instance=profile_img) if form.is_valid(): form.save() return HttpResponseRedirect( reverse('profile_img', kwargs={ 'username': request.user.username, 'id': profile_img.id})) else: form = Profile_ImageEditForm() return render_to_response('accounts/profile_img_edit.html', { 'form':form, 'object':profile_img }, context_instance=RequestContext(request))