how can I change(update) an existing avatar to a new one?

3 views
Skip to first unread message

TTear1943

unread,
Apr 1, 2009, 5:37:44 AM4/1/09
to Django users
Hi, I encounter a problem.
In my user models there is an atrrbution: user.avatar = ImageField
('avatar', upload_to=AVATAR_TEMP_DIR, blank=True, null=True)
then i use a modelform as an create user form. And the avatar is
uploaded corrcet. Which upload to AVATAR_TEMP_DIR, then I move the
avatar into AVATAR_ORIGINAL_PATH and make user.avatar._name as
AVATAR_ORIGINAL_PATH % user.id
After this, then I try to change an avatar. So I user another
modelform to update. however, this time the avatar cannot be uploaded
to AVATAR_TEMP_DIR, so I also cannot move the avatar to
AVATAR_ORIGINAL_PATH.
It seems that, after create the user. the AVATAR_TEMP_DIR set to
upload_to is lost, so the program doesn't know where to upload the
file.
Can anyone help me to solve this problem? Thank you very much.

class UserForm(forms.ModelForm):
class Meta:
model = User
fields = ('name', 'user_avatar', 'description')

class UserAvatarForm(forms.ModelForm):
class Meta:
model = User
fields = ('user_avatar')

@login_required
def user_create(request):
if request.method == 'POST':
form = UserForm(request.POST, request.FILES)
if form.is_valid():
user = form.save(commit=False)
user.save()
user.handle_upload_user_avatar() # move image from
AVATAR_TEMP_DIR to AVATAR_ORIGINAL_PATH
user.save()
return HttpResponseRedirect(user.url)

else:
form = UserForm()

return render_to_response('user/user_create.html', locals())

@login_required
def settings_user_avatar(request, user_name):
user = get_object_or_404(User, name=user_name)
if request.method == 'POST':
form = UserAvatarForm(request.POST, request.FILES,
instance=user)
if form.is_valid():
user = form.save()
user.handle_upload_user_avatar() # move image from
AVATAR_TEMP_DIR to AVATAR_ORIGINAL_PATH
user.save()
return HttpResponseRedirect(user.url)
else:
form = UserAvatarForm(instance=user)

return render_to_response('user/user_avatar.html', locals())

TTear1943

unread,
Apr 1, 2009, 9:44:56 AM4/1/09
to Django users
I seems it doesnot work when update an ImageField.

Karen Tracey

unread,
Apr 1, 2009, 11:39:54 AM4/1/09
to django...@googlegroups.com
2009/4/1 TTear1943 <1943.com@gmail.com>

I seems it doesnot work when update an ImageField.

Certainly ImageFields can be updated. 

You've omitted from the code you posted what exactly is done by the function user.handle_upload_user_avatar(), which is my first guess as to where the problem is.  If you want help solving the problem, you will need to include more of what your code is doing (dpaste.com is likely a better place for than inline in email for any large block of code) , and also it would help if you were very specific about the error messages, tracebacks, or whatever you are encountering. 

For example you say "the avatar cannot be uploaded to AVATAR_TEMP_DIR".  Why not?  What exactly happens? 

You also appear to be drawing conclusions from what you are seeing, when you state things like: "It seems that, after create the user. the AVATAR_TEMP_DIR set to upload_to is lost, so the program doesn't know where to upload the file."  I don't believe this conclusion is valid, but I'm not sure what you saw that made you come to that conclusion, so I'm having a hard time guessing what might be the real problem.

Karen
 

TTear1943

unread,
Apr 1, 2009, 10:33:23 PM4/1/09
to Django users
Thank you for you reply. Now I have slove the problem. But the
solution is very strange.
In fact, there is a clean function in my update form, as following:
def clean_user_avatar(self):
if not self.cleaned_data["user_avatar"]:
raise forms.ValidationError("Image Path is in none")
After I remove it, then it works. Maybe there is some problem in this
function, but I cannot figure it out.
Anyway, thanks for your reply in advance. :)

On 4月1日, 下午11时39分, Karen Tracey <kmtra...@gmail.com> wrote:
> 2009/4/1 TTear1943 <1943....@gmail.com>
Reply all
Reply to author
Forward
0 new messages