错误“在使用“dj_rest_auth”时,“/dj-rest-auth/login/”配置不正确”。

huangapple go评论114阅读模式
英文:

Error "ImproperlyConfigured at /dj-rest-auth/login/" in use "dj_rest_auth"

问题

I Want Use packeage 'dj_rest_auth' in django-rest-framework for login but get error:

ImproperlyConfigured at /dj-rest-auth/login/
No default throttle rate set for 'dj_rest_auth' scope

In setting.py File:

INSTALLED_APPS = [
    ...
    # 3rd Party
    'rest_framework',
    'rest_framework.authtoken',
    'rest_framework_simplejwt',
    'dj_rest_auth',

    # local
    ...
]

REST_USE_JWT = True

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES':[
        'rest_framework_simplejwt.authentication.JWTAuthentication',
        'dj_rest_auth.jwt_auth.JWTCookieAuthentication',
    ],
    'DEFAULT_PERMISSION_CLASSES':[
        'rest_framework.permissions.AllowAny'
    ],
    'DEFAULT_RENDERER_CLASSES':[
        'rest_framework.renderers.JSONRenderer'
    ],
    'DEFAULT_THROTTLE_CLASSES':[
        'rest_framework.throttling.AnonRateThrottle',
        'rest_framework.throttling.UserRateThrottle',
        'rest_framework.throttling.ScopedRateThrottle',
    ],
    'DEFAULT_THROTTLE_RATES':{
        'anon':'50/hour',
        'user':'200/hour',
        'register':'5/hour',
        'ref_token':'5/minute',
        'create_article':'5/minute',
    }
}

And url.py:

urlpatterns = [
    ...
    path('dj-rest-auth/', include('dj_rest_auth.urls')),
]
英文:

I Want Use packeage 'dj_rest_auth' in django-rest-framework for login but get error:

ImproperlyConfigured at /dj-rest-auth/login/
No default throttle rate set for 'dj_rest_auth' scope

In setting.py File:

INSTALLED_APPS = [
    ...
    # 3rd Party
    'rest_framework',
    'rest_framework.authtoken',
    'rest_framework_simplejwt',
    'dj_rest_auth',

    # local
    ...
]


REST_USE_JWT = True

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES':[
        'rest_framework_simplejwt.authentication.JWTAuthentication',
        'dj_rest_auth.jwt_auth.JWTCookieAuthentication',
    ],
    'DEFAULT_PERMISSION_CLASSES':[
        'rest_framework.permissions.AllowAny'
    ],
    'DEFAULT_RENDERER_CLASSES':[
        'rest_framework.renderers.JSONRenderer'
    ],
    'DEFAULT_THROTTLE_CLASSES':[
        'rest_framework.throttling.AnonRateThrottle',
        'rest_framework.throttling.UserRateThrottle',
        'rest_framework.throttling.ScopedRateThrottle',
    ],
    'DEFAULT_THROTTLE_RATES':{
        'anon':'50/hour',
        'user':'200/hour',
        'register':'5/hour',
        'ref_token':'5/minute',
        'create_article':'5/minute',
    }
}

And url.py:

urlpatterns = [
...
path('dj-rest-auth/',include('dj_rest_auth.urls')),
]

答案1

得分: 0

将以下内容添加到您的settings.py文件中:

'DEFAULT_THROTTLE_RATES': { 
    'dj_rest_auth': '10000/day',
    ...
},

或者在视图中设置throttle_scope为其他值,比如anon

在项目代码中遇到了相同的问题,并找到了这行代码:

https://github.com/iMerica/dj-rest-auth/blob/4b3a4ce846c1fd358dfb1ff9fa3fdc8120d884f9/dj_rest_auth/views.py#L274

该视图设置了一个自定义的节流范围,在settings.py文件的REST_FRAMEWORK部分中您可能没有配置,因此出现了错误。不清楚为什么他们会进行这种更改并且没有正确记录它。

英文:

TL;DR
Add this into your settings.py file

'DEFAULT_THROTTLE_RATES': { 
    'dj_rest_auth': '10000/day',
    ...
},

or set the throttle_scope within the view to something else like anon.

Experienced the same issue and found this line within the project code

https://github.com/iMerica/dj-rest-auth/blob/4b3a4ce846c1fd358dfb1ff9fa3fdc8120d884f9/dj_rest_auth/views.py#L274

The view sets a custom throttle scope which you would not have configured within the REST_FRAMEWORK section of the settings.py file hence the error. No idea why they would make this change and not document it correctly.

huangapple
  • 本文由 发表于 2023年1月9日 01:31:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定