首页 > 解决方案 > 为什么 Django 更新对象第一次没有触发?

问题描述

我有两个功能。我的第一个函数将布尔值更新为 True 为 False。我的第一个函数正常工作,但我的第二个函数没有将对象 False 更新为 True。如果我尝试两次然后第二次我的第二个函数将对象 False 更新为 True。我不明白为什么它第一次不更新?

这是我的代码:

function1 #这个函数正常工作

视图.py

def ForgetPassword(request):
    if request.method == "POST":
    .....my others code
   profile_obj = UserProfile.objects.get(user=user_obj)
      
   profile_obj.email_confirmed = False
   profile_obj.save() 
   .....my others code

function2 #this 函数无法正常工作 如果我尝试两次然后第二次我的 this 函数将对象 False 更新为 True:

class ChangePassword_link(View):
        #my others code....

        if user is not None and account_activation_token.check_token(user, token):
            profile_obj = UserProfile.objects.filter(user=user).update(email_confirmed = True)
            messages.success(request, ('Your account have been confirmed. Now you can login'))
            return redirect('members:change-password-page')
                
        else:
            messages.warning(request, ('The confirmation link was invalid, possibly because it has already been used.'))
            return redirect('members:change-password-page') 
        
       #my others code....

标签: pythonpython-3.xdjango

解决方案


推荐阅读