Django csrf在注销登录新用户后失败

Django csrf在注销登录新用户后失败,django,django-csrf,Django,Django Csrf,即使我理解这个问题,我也不知道如何解决这个问题。我有一个支持django的api,它有一个端点,允许用户更改电子邮件。如果登录用户A输入一封已存在的电子邮件,它会检查登录用户A是否输入了与已存在的用户B对象相对应的密码(即他拥有另一个较旧的帐户)。如果是这样,我必须注销实际用户A,然后再次登录已经存在的B帐户 ... if User.objects.filter(email=email).exists(): # If the email already belongs to anothe

即使我理解这个问题,我也不知道如何解决这个问题。我有一个支持django的api,它有一个端点,允许用户更改电子邮件。如果登录用户A输入一封已存在的电子邮件,它会检查登录用户A是否输入了与已存在的用户B对象相对应的密码(即他拥有另一个较旧的帐户)。如果是这样,我必须注销实际用户A,然后再次登录已经存在的B帐户

...
if User.objects.filter(email=email).exists():
    # If the email already belongs to another account
    user = authenticate(username=email, password=password)
    if user is not None:
        # The user is the owner of the existing account, he has the password
        # Get already existing client object
        client_existing_user_obj = Client.objects.get(user=user)

        # get clients actual cart
        actual_cart = client.cart
        # update existing clients cart with newly created cart
        client_existing_user_obj.cart = actual_cart

        # logout user with the old email, login already existing user
        logout(request)
        login(request, user)

        ...
端点工作正常,返回200。但是,下一个post&put请求回答403-“详细信息”:“CSRF失败:CSRF令牌丢失或不正确。”

我怎样才能解决这个问题?当用户登录时,任何建议都会有所帮助。

Django。这是一项安全措施

在提交更多POST/PUT请求之前,您必须在登录后刷新令牌(例如刷新页面)