【发布时间】:2021-09-13 10:15:20
【问题描述】:
我遇到的错误
AttributeError: 模块 'django.http.request' 没有属性 'user'
我尝试在用户登录时发送的信号
def auth_done(sender, **kwargs):
print('User has logged in')
tok = Token.objects.create(user=request.user)
print(tok.key())
user_logged_in.connect(auth_done, sender=User)
登录视图
@csrf_exempt
def login_in(request):
if request.method == 'POST':
name = request.POST['first_name']
password = request.POST['password']
user = authenticate(username=name, password=password)
if user is not None:
print(user)
login(request, user)
tok = Token.objects.create(user=request.user)
print(tok.key)
print('User is authenticated')
else:
print('Not authenticated')
return render(request, 'Auth/user.html')
设置
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
],
# Use Django's standard `django.contrib.auth` permissions,
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.isAuthenticated'
]
}
【问题讨论】:
标签: django django-rest-framework django-authentication