自定义当用户/密码不正确时检索到的默认消息djangorestframeworksimple jwt?
我正在使用django 3.0.5、djangorestframework 3.11.0和djangorestframework simplejwt 4.4.0 我已经使用drf simple jwt进行了身份验证,所有的工作都很好。当密码不正确时,响应为自定义当用户/密码不正确时检索到的默认消息djangorestframeworksimple jwt?,django,rest,authentication,django-rest-framework,django-rest-framework-simplejwt,Django,Rest,Authentication,Django Rest Framework,Django Rest Framework Simplejwt,我正在使用django 3.0.5、djangorestframework 3.11.0和djangorestframework simplejwt 4.4.0 我已经使用drf simple jwt进行了身份验证,所有的工作都很好。当密码不正确时,响应为 {"detail":"No active account found with the given credentials"} 我需要自定义此响应。我已经在TokenActainSerializer类的字典中检查了此消息 defaul
{"detail":"No active account found with the given credentials"}
我需要自定义此响应。我已经在TokenActainSerializer类的字典中检查了此消息
default_error_messages = {
'no_active_account': _('No active account found with the given credentials')
}
我试图重写这个类,但没有成功
有什么想法吗?
提前感谢这可以通过创建自定义序列化程序和视图来实现 序列化程序.py
# Import django packages
from django.utils.translation import gettext_lazy as _
# Import external packages
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer as SimpleTokenObtainPairSerializer
class TokenObtainPairSerializer(SimpleTokenObtainPairSerializer):
default_error_messages = {
'no_active_account': _('CUSTOM ERROR MESSAGE HERE')
}
views.py
# Import external packages
from rest_framework_simplejwt.views import TokenObtainPairView as SimpleTokenObtainPairView
# Import my packages
from gadget.auth.serializers import TokenObtainPairSerializer
class TokenObtainPairView(SimpleTokenObtainPairView):
serializer_class = TokenObtainPairSerializer
最后,更新URL以使用新视图
url.py
# Import external packages
from rest_framework_simplejwt.views import TokenRefreshView
# Import my packages
from .views import TokenObtainPairView
urlpatterns = [
# Token authentication
path(r'token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path(r'token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
]
这可以通过创建自定义序列化程序和视图来实现 序列化程序.py
# Import django packages
from django.utils.translation import gettext_lazy as _
# Import external packages
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer as SimpleTokenObtainPairSerializer
class TokenObtainPairSerializer(SimpleTokenObtainPairSerializer):
default_error_messages = {
'no_active_account': _('CUSTOM ERROR MESSAGE HERE')
}
views.py
# Import external packages
from rest_framework_simplejwt.views import TokenObtainPairView as SimpleTokenObtainPairView
# Import my packages
from gadget.auth.serializers import TokenObtainPairSerializer
class TokenObtainPairView(SimpleTokenObtainPairView):
serializer_class = TokenObtainPairSerializer
最后,更新URL以使用新视图
url.py
# Import external packages
from rest_framework_simplejwt.views import TokenRefreshView
# Import my packages
from .views import TokenObtainPairView
urlpatterns = [
# Token authentication
path(r'token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path(r'token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
]