我有一個觀點,使用戶能夠編輯他們的個人資料(常用名稱,用戶名等等)和UserProfile
內包含在ÌmageField
一個圖像:Django的傳遞request.POST和request.FILES在editprofile形式
@login_required
def editprofile(request):
user = request.user
if request.method == 'POST':
edit_form = EditProfileForm(data = request.POST, user = user)
if edit_form.is_valid():
user = edit_form.save()
request.user.message_set.create(message='Votre profil a été modifié.')
return HttpResponseRedirect('/')
else:
dict = {'first_name':user.first_name, 'last_name':user.last_name, 'email':user.email, 'username':user.username}
edit_form = EditProfileForm(user = user, data = dict)
tpl_dict = {'form' : edit_form}
return render_to_response('editprofile.html', tpl_dict, RequestContext(request))
和形式是:
class EditProfileForm(forms.Form):
first_name = forms.CharField(max_length = 100, required=False)
last_name = forms.CharField(max_length = 100, required=False)
email = forms.EmailField()
username = forms.CharField(max_length = 100)
avatar = forms.ImageField(required = False)
def __init__(self, user, *args, **kwargs):
super(EditProfileForm, self).__init__(*args, **kwargs)
self.user = user
def save(self):
user = self.user
user.email = self.cleaned_data['email']
user.username = self.cleaned_data['username']
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.save()
profile = user.get_profile()
profile.avatar = self.cleaned_data['avatar']
profile.save()
return user
的問題是,我需要傳遞`request.FILES」的形式! 我試了
edit_form = EditProfileForm(data = request.POST, request.FILES, user = user)
和其他變種沒有成功。
這將等同於'EditProfileForm(用戶= request.POST,文件= request.FILES,用戶=用戶)' - 引發異常(多收到「用戶」的價值)。 – eternicode 2011-01-27 19:34:20