【发布时间】:2017-11-29 03:33:44
【问题描述】:
我如何知道 request.user 来自哪里?
我有一个 TestRequestUserAPIView:
class TestRequestUserAPIView(View):
def dispatch(self, request, *args, **kwargs):
result = super(TestRequestUserAPIView, self).dispatch(request, *args, **kwargs)
return result
def get(self, request):
user = request.user # this line I get the user (who access the API)
return HttpResponse("ok")
当它执行这一行时user = request.user。
我可以得到请求user(谁请求这个API)。
我想知道用户如何生成请求,为什么我在 Chrome 等浏览器中请求这个 API,我的请求会有用户属性?
是通过 cookie 吗?或一些令牌(在我的测试项目中,我登录了。但是我在访问API时没有将令牌放入请求中,仍然在后端reqest.user中获取用户)?
编辑 -1
我的项目中有 django build-in auth 和 rest-auth:
下面的身份验证在我的 INSTALLED_APPS 中:
'django.contrib.auth',
'rest_auth',
'allauth',
'allauth.account',
'allauth.socialaccount',
'rest_auth.registration',
我也想知道前端通过什么来识别用户,它使用cookie吗?还是令牌?我在登录时使用 rest-auth 生成令牌。
【问题讨论】: