djangorest框架csrf token 丢失或不正确

标签 django django-rest-framework django-rest-auth

你好,我正在使用 django Rest-auth,我在/password/change/中遇到这个问题,它总是返回 csrf token 丢失或不正确的问题: 我正在向我正在开发的 Android 应用程序发出请求 我的设置是:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',

'rest_framework',
'rest_framework.authtoken',

'rest_auth',
'rest_auth.registration',

'allauth',
'allauth.account',
'allauth.socialaccount',

]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    )
}

版本: django-rest-auth==0.9.1

djangorestframework==3.6.2

最佳答案

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
    'rest_framework.authentication.TokenAuthentication',
)
}

从 DEFAULT_AUTHENTICATION_CLASSES 中删除 'rest_framework.authentication.SessionAuthentication',如果您仍需要 DRF 可浏览的 api View ,请使用 ModHeader在 Chrome 中。

关于djangorest框架csrf token 丢失或不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43783049/

相关文章:

django-rest-auth 错误 - save() 采用 1 个位置参数,但给出了 2 个

python - Django国家不显示国家列表

django - NoReverseMatch错误

python - 如何添加已经存在的db字段中的数据

python - 如何反序列化 django rest 框架中的多个对象列表?

python - DRF - 发布 JSON 对象时日期值出现 KeyError

django - 从 django-rest-auth 获取 React 中的用户详细信息会返回 403(禁止访问)- 使用 authtoken

Django allauth : How to override the confirmation email url

Django 无法立即获得组的许可

python - django模型字段中字符串参数的含义是什么?