【发布时间】:2021-09-05 18:45:37
【问题描述】:
我基本上是 DRF 的新手,并试图通过移动应用程序创建一个 API 端点来更新用户,这是我的代码,
class MobileAppUserUpdateView(APIView):
def post(self, request, format=None):
user = request.user
print('USER',user) #<- prints nothing
print('TOKEN',request.auth) #<- prints the token
return ResponseBuilder.success([constants.RECORD_UPDATED])
我想访问使用身份验证令牌发出该请求但在那里一无所获的用户。这是我的 DRF 设置,
NON_FIELD_ERRORS_KEY = 'message'
REST_FRAMEWORK = {
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
# 'rest_framework.authentication.TokenAuthentication',
],
'DEFAULT_AUTHENTICATION_CLASSES': (
# 'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.TokenAuthentication',
),
'NON_FIELD_ERRORS_KEY': NON_FIELD_ERRORS_KEY,
}
我还根据文档在INSTALLED_APPS 列表中添加了"rest_framework.authtoken"。任何帮助将不胜感激...
【问题讨论】:
标签: python django django-rest-framework