我刚刚将此添加到了我的settings.py:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication', **<--THIS!**
'knox.auth.TokenAuthentication',
),
}
现在,当我尝试为用户提供令牌时,我会收到此消息:
“详细信息”:“无效的用户名/密码。”
但是我不尝试登录,只能让用户拥有令牌。
我正在尝试获取由用户api / todo创建的所有待办事项,并获取用户api / auth / user ..但我一直收到此错误:
{
"detail": "Authentication credentials were not provided."
}
我在前端发送标头(在正文中进行了尝试,在查询中没有任何作用)
const config = {headers: {Authorization: `Token ${token}`}}
所以我尝试在邮递员的“标题”选项卡中发送令牌-Authorization:Token fcabd79eb8f0954d9d29d48059ab38853c7f79c582aa2653a16b4b9e176b0225
我得到同样的错误。
这是我在前端的服务
async function query() {
try {
// tried both this:
return await axios.get(`http://127.0.0.1:8000/api/${types.TODO_API}/`,{headers : config.headers});
// and this:
return await axios.get(`http://127.0.0.1:8000/api/${types.TODO_API}/`, null, {headers : config.headers});
} catch (err) {
throw err;
};
};
这是我的设定。py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'corsheaders',
'todo',
'user',
'knox'
]
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'knox.auth.TokenAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
}
这是TodoViewSet
class TodoViewSet(viewsets.ModelViewSet):
permission_classes = (permissions.IsAuthenticated, )
serializer_class = TodoSerializer
def get_queryset(self):
return self.request.user.todo.all()
def preform_create(self, serializer):
serializer.save(owner = self.request.user)
这是GetUserView
class GetUserView(generics.RetrieveAPIView):
permission_classes = (permissions.IsAuthenticated,)
serializer_class = UserSerializer
def get_user(self):
return self.request.user