我试图修改当前用户的数据,但没有成功,需要一些帮助。
def account_admin(request):
if request.method == 'POST':
mod_form = ModificationForm(request.POST)
if mod_form.is_valid():
user = User.objects.get(request.user)
user.set_password(form.cleaned_data['password1'])
user.email = form.cleaned_data['email']
user.save
return HttpResponseRedirect('/register/success/')
else:
mod_form = ModificationForm()
variables = RequestContext(request, {
'mod_form': mod_form
})
return render_to_response('registration/account.html', variables)
答案 0 :(得分:2)
request.user
已经是User的一个实例,进行另一个查询没有意义。
另外,您实际上需要调用 save()
。
答案 1 :(得分:1)
您的问题在这里:
user = User.objects.get(request.user)
理想情况下,它本来是
user = User.objects.get(id=request.user.id)
您不需要查询来在此处检索用户对象,因为request.user
评估为登录用户对象的实例。
user = request.user
user.set_password(form.cleaned_data['password1'])
user.email = form.cleaned_data['email']
user.save()
应该工作
另外,请确保您拥有account_admin
方法的@login_required
装饰器
答案 2 :(得分:0)
您似乎从上述帖子中回答,但我注意到您没有对用户进行身份验证(您可能已经在其他地方进行了我不知道的事情)。如果您想这样做,请尝试使用login_reqired装饰器https://docs.djangoproject.com/en/1.5/topics/auth/default/#django.contrib.auth.decorators.login_required
答案 3 :(得分:0)
非常感谢karthikr和Daniel!以下是我如何让它工作:
def account_admin(request):
if request.method == 'POST':
mod_form = ModificationForm(request.POST)
if mod_form.is_valid():
user = User.objects.get(id=request.user.id)
user.set_password(mod_form.cleaned_data['password1'])
user.email = mod_form.cleaned_data['email']
user.save()
return HttpResponseRedirect('/register/success/')
else:
mod_form = ModificationForm()
variables = RequestContext(request, {
'mod_form': mod_form
})
return render_to_response('registration/account.html', variables)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
karthikr,我还在学习django,我不会让你对装饰工具@login_required感到厌烦。它做了什么?