0
目前我正在手動生成令牌,但我想使用jwt
令牌,我遵循官方文檔和其他參考,但我仍然無法找出問題所在。獲取DRF-jwt令牌
serializers.py,其中手動生成驗證令牌後。
class UserLoginSerializer(serializers.ModelSerializer):
token = serializers.CharField(allow_blank=True, read_only=True)
class Meta:
model = User
fields = [
'username',
'password',
'token',
]
extra_kwargs = {"password":
{"write_only": True}
}
def validate(self, data):
username = data.get('username', None)
password = data.get('password', None)
try:
usern = Account.objects.get(username=username)
except ObjectDoesNotExist:
raise serializers.ValidationError("User does not exists")
if usern.check_password(password):
data["token"] = "asdasdasdasd"
else:
raise serializers.ValidationError("password invalid")
return data
urls.py
from django.conf.urls import url
from .views import AuthRegister, AuthLogin
from rest_framework_jwt.views import obtain_jwt_token
urlpatterns = [
url(r'^register/$', AuthRegister.as_view()),
url(r'^login/$', AuthLogin.as_view()),
url(r'^api-token-auth/', obtain_jwt_token),
]
在設置我已經包括'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
我用url(r'^api-token-auth/', obtain_jwt_token)
,但我無法弄清楚我將如何產生jwt
令牌。 請任何人幫助我!