Djangorestframework 记录一个报错 -- rest_framework.authentication.ToKenAuthentication

今天在使用 Djangorestframework 这个框架时,发生报错:

ImportError: Could not import 'rest_framework.authentication.ToKenAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'. 
ImportError: Module "rest_framework.authentication" does not define a "ToKenAuthentication" attribute/class.

看报错信息就知道,是 settings 设置的问题,我是这样写的

'DEFAULT_AUTHENTICATION_CLASSES': [
        # 认证
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.ToKenAuthentication',
    ]

没毛病啊,是单词写错了? 仔细看了好久,没发现什么问题,然后就开启百度模式,搜了好久,搜了很多方法,都没用。

凭借多年以来处理问题的方法,遇到事情不要慌,抽根烟冷静下(此处并非教唆大家去抽烟,大家可以忽略这一句),抽了根烟,喝了杯茶,然后脑子突然灵光一现,发现了问题

以下是我改正后的代码

'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.BasicAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    )

改完后,一运行,哎呀,这问题就解决了  ......

原来这个地方应该元祖来写配置,不能用列表。

原文地址:https://www.cnblogs.com/shiyixirui/p/14495189.html