request.user.is_authenticated总是返回false [英] request.user.is_authenticated always returns false

查看:432
本文介绍了request.user.is_authenticated总是返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

登录用户并检查request.user是否在Android中的其他活动中被身份验证。该值始终为false。



以下代码用于登录用户

  from rest_framework.response import Response 
from rest_framework import status
from rest_framework.decorators import api_view
from django.contrib.auth.models import User
from django.contrib .auth import authenticate,logout,login


@api_view(['POST'])
def userRegister(request):
user = User.objects.create_user (username = request.POST ['username'],email = request.POST ['email'],password = request.POST ['password'])
return响应({'ok':'True'} ,status = status.HTTP_201_CREATED)

@api_view(['POST'])
def userLogin(request):
user = authenticate(
username = request。 POST ['username'],
password = request.POST ['password']

如果用户不是无:
登录(请求,用户)
return Response({'ok':'True'},status = status.HT TP_200_OK)
else:
return响应({'ok':'False'},status = status.HTTP_401_UNAUTHORIZED)

以下代码用于检查用户是否经过身份验证

  from rest_framework .response import Response 
from rest_framework.decorators import api_view
from。从
导入模型。导入串行器
从django.contrib.auth.models import用户
从rest_framework导入状态


@api_view(['GET'])
def HomeView(request):
如果request.user.is_authenticated:
return响应(data = {ok:'true'})
else:
返回响应(data = {ok:false})


解决方案

您的观点(如果您使用的是Django< 1.10版本,作为@Kevin Christopher Henry在下面的评论中指出),您需要执行以下操作:

  if request.user.is_authenticated():

如果你执行 .is_authenticated (没有括号),它将返回方法本身而不是布尔值( True False ),并且将始终评估为 True ,或至少应该。我不知道为什么会在你的情况下返回 False ,但我认为这应该可以解决你的问题。



另外请注意,在Django模板中,情况是不同的。在一个模板中,您可以(并且必须)执行以下操作:

  {%if user.is_authenticated%} 
is_authenticated
之后缺少圆括号,c $ c>

如果您在模板案例中包含括号,您将收到错误,而如果您不想将包含在视图中,则会出现意想不到的结果。


after logging in the user and checking whether request.user isauthenticated in other activity in android. the value is always false.

the following code is used for login a user

from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import api_view
from django.contrib.auth.models import User
from django.contrib.auth import authenticate,logout,login


@api_view(['POST'])
def userRegister(request):
    user=User.objects.create_user(username=request.POST['username'],email=request.POST['email'],password=request.POST['password'])
    return Response({'ok':'True'},status=status.HTTP_201_CREATED)

@api_view(['POST'])
def userLogin(request):
    user=authenticate(
        username=request.POST['username'],
        password=request.POST['password']
    )
    if user is not None:
        login(request,user)
        return Response({'ok':'True'},status=status.HTTP_200_OK)
    else:
        return Response({'ok':'False'},status=status.HTTP_401_UNAUTHORIZED)

the following code is used to check whether the user is authenticated or not

from rest_framework.response import Response
from rest_framework.decorators import api_view
from . import models
from . import serializers
from django.contrib.auth.models import User
from rest_framework import status


@api_view(['GET'])
def HomeView(request):
    if request.user.is_authenticated:
        return Response(data={"ok":'true'})
    else:
        return Response(data={"ok":"false"})

解决方案

In your view (if you are using a version of Django < 1.10, as @Kevin Christopher Henry points out in the comments below) you need to do:

if request.user.is_authenticated():

If you do .is_authenticated (without the parentheses) it will return the method itself instead of a boolean (True or False), and will always evaluate to True, or at least it should. I am not sure why it would return False in your case, but I think this should solve your problem.

Also, note that in Django templates the situation is different. In a template you can (and must) do:

{% if user.is_authenticated %} 

Note the lack of parentheses after is_authenticated. If you include parentheses in the template case, you will get an error, while if you don't include them in the view case you will get unexpected results.

这篇关于request.user.is_authenticated总是返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆