本文整理汇总了Python中forms.UserProfileForm.user方法的典型用法代码示例。如果您正苦于以下问题:Python UserProfileForm.user方法的具体用法?Python UserProfileForm.user怎么用?Python UserProfileForm.user使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类forms.UserProfileForm
的用法示例。
在下文中一共展示了UserProfileForm.user方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: user_profile
# 需要导入模块: from forms import UserProfileForm [as 别名]
# 或者: from forms.UserProfileForm import user [as 别名]
def user_profile(request):
user = User.objects.get_or_create(pk=request.user.id)
if request.method == 'POST':
form = UserProfileForm(request.POST, instance=request.user)
if form.is_valid():
form.save()
form.user = request.user
return HttpResponseRedirect('/accounts/loggedin')
else:
form= UserProfileForm(instance=request.user)
args={}
args.update(csrf(request))
args['form'] = form
return render_to_response('profile.html',args, context_instance=RequestContext(request))
#
# # def user_profile(request):
# success = False
# user = User.objects.get(pk=request.user.id)
# if request.method == 'POST':
# upform = UserProfileForm(request.POST, instance=user.get_profile())
# if upform.is_valid():
# up = upform.save(commit=False)
# up.user = request.user
# up.save()
# success = True
# else:
# upform = UserProfileForm(instance=request.user)
#
# return render_to_response('profile.html',
# locals(), context_instance=RequestContext(request))
#
示例2: profile
# 需要导入模块: from forms import UserProfileForm [as 别名]
# 或者: from forms.UserProfileForm import user [as 别名]
def profile(request):
if request.method == "POST":
if request.user.is_authenticated():
try:
u=User.objects.get(username=request.user.username)
except User.DoesNotExist:
u = None
print u
up=UserProfile.objects.get_or_create(user=u)[0]
print up
user_profile_form=UserProfileForm(data=request.POST)
user_profile_form.user=u
if user_profile_form.is_valid():
print "valid form"
if 'avatar' in request.FILES:
up.location=request.POST['location']
#up.avatar=request.FILES['avatar'],
up.user_skills=request.POST['skills']
up.save()
image=request.FILES['avatar']
print image.content_type
print image.size
if image.content_type in ["image/jpeg","image/png","image/jpg"] and (image.size/1024) <= 1024:
up.avatar.save(image.name,image)
else:
return render_to_response('after_profile_update.html',
{"message": "file type is invalid or size exceeds 1 MB"},
RequestContext(request))
else:
up.location=request.POST['location']
up.user_skills=request.POST['skills']
up_avatar=up.avatar
up.avatar=up_avatar
up.save()
return render_to_response(
'after_profile_update.html',
{"message": "Your profile has been updated"},
RequestContext(request))
else:
print "the form submitted was invalid"
print user_profile_form.errors
#this handles the ValidationError raised in forms.py
return render_to_response('after_profile_update.html',
{"message": "please enter valid data.The location and skills field are required. Profile photo is optional"},
RequestContext(request))
else:
#the user has to login to post and is displayed the login to post message if he does so without logging in
return HttpResponse("login to post")
# displaying the form for the first time.
else:
user_profile_form = UserProfileForm()
return render_to_response(
'update_profile.html',
{'user_profile_form': user_profile_form},
RequestContext(request))