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

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

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:

  1. INSTALLED_APPS = [
  2. ...
  3. # 3rd Party
  4. 'rest_framework',
  5. 'rest_framework.authtoken',
  6. 'rest_framework_simplejwt',
  7. 'dj_rest_auth',
  8. # local
  9. ...
  10. ]
  11. REST_USE_JWT = True
  12. REST_FRAMEWORK = {
  13. 'DEFAULT_AUTHENTICATION_CLASSES':[
  14. 'rest_framework_simplejwt.authentication.JWTAuthentication',
  15. 'dj_rest_auth.jwt_auth.JWTCookieAuthentication',
  16. ],
  17. 'DEFAULT_PERMISSION_CLASSES':[
  18. 'rest_framework.permissions.AllowAny'
  19. ],
  20. 'DEFAULT_RENDERER_CLASSES':[
  21. 'rest_framework.renderers.JSONRenderer'
  22. ],
  23. 'DEFAULT_THROTTLE_CLASSES':[
  24. 'rest_framework.throttling.AnonRateThrottle',
  25. 'rest_framework.throttling.UserRateThrottle',
  26. 'rest_framework.throttling.ScopedRateThrottle',
  27. ],
  28. 'DEFAULT_THROTTLE_RATES':{
  29. 'anon':'50/hour',
  30. 'user':'200/hour',
  31. 'register':'5/hour',
  32. 'ref_token':'5/minute',
  33. 'create_article':'5/minute',
  34. }
  35. }

And url.py:

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

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:

  1. INSTALLED_APPS = [
  2. ...
  3. # 3rd Party
  4. 'rest_framework',
  5. 'rest_framework.authtoken',
  6. 'rest_framework_simplejwt',
  7. 'dj_rest_auth',
  8. # local
  9. ...
  10. ]
  11. REST_USE_JWT = True
  12. REST_FRAMEWORK = {
  13. 'DEFAULT_AUTHENTICATION_CLASSES':[
  14. 'rest_framework_simplejwt.authentication.JWTAuthentication',
  15. 'dj_rest_auth.jwt_auth.JWTCookieAuthentication',
  16. ],
  17. 'DEFAULT_PERMISSION_CLASSES':[
  18. 'rest_framework.permissions.AllowAny'
  19. ],
  20. 'DEFAULT_RENDERER_CLASSES':[
  21. 'rest_framework.renderers.JSONRenderer'
  22. ],
  23. 'DEFAULT_THROTTLE_CLASSES':[
  24. 'rest_framework.throttling.AnonRateThrottle',
  25. 'rest_framework.throttling.UserRateThrottle',
  26. 'rest_framework.throttling.ScopedRateThrottle',
  27. ],
  28. 'DEFAULT_THROTTLE_RATES':{
  29. 'anon':'50/hour',
  30. 'user':'200/hour',
  31. 'register':'5/hour',
  32. 'ref_token':'5/minute',
  33. 'create_article':'5/minute',
  34. }
  35. }

And url.py:

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

答案1

得分: 0

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

  1. 'DEFAULT_THROTTLE_RATES': {
  2. 'dj_rest_auth': '10000/day',
  3. ...
  4. },

或者在视图中设置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

  1. 'DEFAULT_THROTTLE_RATES': {
  2. 'dj_rest_auth': '10000/day',
  3. ...
  4. },

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:

确定