0
我正在做一個djano-web fromework項目。提交django表單時發生了一些奇怪的事情。一切都變得越來越接受個人資料圖片在db中雖然形式有效。個人資料圖像未保存在數據庫中
views.py:
class EmployeeEditProfile(CommonViewMixin, View):
template_name = 'edit_employee.html'
authorization = [authorize_employee]
user_personal_form = UserPersonalDetailForm
user_details_form = OrganisationUserDetailsForm
user_profile_form = OrganisationUserProfileDetailsForm
@transaction.atomic()
def non_ajax_post(self, request, *args, **kwargs):
user_personal_form = self.user_personal_form(request.POST, instance=request.user)
user_details_form = self.user_details_form(request.POST, instance=request.user.organisation_user.user_details)
user_profile_form = self.user_profile_form(request.POST,request.FILES, instance=request.user.organisation_user)
if user_details_form.is_valid() and user_personal_form.is_valid() and user_profile_form.is_valid():
user_personal_form.save()
user_detail = user_details_form.save()
request.user.organisation_user.user_details = user_detail
request.user.organisation_user.save()
user_profile_form.save()
request.session['user_form_saved'] = True
return HttpResponseRedirect(request.path)
else:
print user_details_form.errors
print user_personal_form.errors
context = kwargs
context.update({'user_personal_form': user_personal_form, 'user_details_form': user_details_form, 'user_profile_form':user_profile_form})
return self.response_class(template='edit_employee.html',request=request,
context=context)
模型UserProfileDetails:models.py
class OrganisationUserProfileDetails(models.Model):
profile_image = models.ImageField(upload_to='profile_images', null=True, blank=True, default='default_images/profile_pic')
organisation_user = models.OneToOneField(OrganisationUser, related_name='profile_details')
forms.py:
class OrganisationUserProfileDetailsForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(OrganisationUserProfileDetailsForm, self).__init__(*args, **kwargs)
self.fields['profile_image'].label_from_instance = lambda obj: '%s' %(obj.name)
class Meta:
model = OrganisationUserProfileDetails
fields = ('profile_image',)
任何幫助將是明顯的。
不過它不工作,甚至節約organisation_user。 –
我已經改變了views.py一點點,請看看。 –
我已將實例傳遞給user_profile_form –