本文整理汇总了Python中forms.UserProfileForm.clean方法的典型用法代码示例。如果您正苦于以下问题:Python UserProfileForm.clean方法的具体用法?Python UserProfileForm.clean怎么用?Python UserProfileForm.clean使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类forms.UserProfileForm
的用法示例。
在下文中一共展示了UserProfileForm.clean方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: editProfile
# 需要导入模块: from forms import UserProfileForm [as 别名]
# 或者: from forms.UserProfileForm import clean [as 别名]
def editProfile(request):
if request.user.is_authenticated():
form =UserProfileForm(request.POST, request.FILES)
if form.is_valid():
cleaned_data = form.clean()
#extract form info (could potentially use .serialize() to remove redundant code)
first_name = cleaned_data['first_name'].strip()
last_name = cleaned_data['last_name'].strip()
email = cleaned_data['emailAddress']
dorm = cleaned_data['dorm']
pic = request.FILES.get('pic','')
#update user fields
user = User.objects.get(username = request.user)
userdata = user.userdata
user.first_name = first_name
user.last_name = last_name
user.email = email
userdata.dorm = dorm
if pic:
userdata.pic = pic
#save results
user.save()
userdata.save()
#resubmit user data to pre-fill form
data = {'first_name':user.first_name, 'last_name':user.last_name, 'emailAddress':user.email, 'dorm':userdata.dorm}
form = UserProfileForm(initial=data)
return render(request, 'editProfile.html', {'form':form})
else:
#if a form error occurs
user = request.user
userdata = user.userdata
#re-render the form - the html page will handle the error display
data = {'first_name':user.first_name, 'last_name':user.last_name, 'emailAddress':user.email, 'dorm':userdata.dorm}
form = UserProfileForm(initial = data)
return render(request, 'editProfile.html', {'form':form})
else:
return HttpResponseRedirect('/login')