我正在为每个登录用户生成一个令牌。但是通过 Postman 我得到这个错误的请求。请指导我什么是错的!
Postman 错误屏幕截图
我的models.py
class allmodelfields(AbstractBaseUser):
USERNAME_FIELD = 'email'
#id_no = models.BigAutoField(primary_key=True,serialize=False,verbose_name="ID",)
email = models.EmailField(verbose_name='Email',max_length=255,unique=True,)
password = models.CharField(max_length=255)
confirm_password = models.CharField(max_length=255)
first_name = models.CharField(max_length=30)
middle_name = models.CharField(max_length=30,blank = True)
last_name = models.CharField(max_length=30,blank = True)
number_code = models.CharField(max_length=10)
phone_no= models.CharField(max_length=10)
pincode = models.CharField(max_length=10)
house_no = models.CharField(max_length=50)
street = models.CharField(max_length=50,blank = True)
locality = models.CharField(max_length=50,blank = True)
state = models.CharField(max_length=50)
city = models.CharField(max_length=50)
country = models.CharField(max_length=50)
ip = models.CharField(blank = True,max_length = 20)
# auto filled fields
full_name = models.CharField(max_length=100,)#default = first_name + middle_name + last_name)
full_number = models.CharField(max_length = 23)#f'{number_code} {phone_no}'
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
is_ active = models.BooleanField(default=False)
unique_id = models.CharField(max_length = 10, editable=False)
def __str__(self):
return self.email
def full_name_func(self):
return f'{self.first_name} {self.middle_name} {self.last_name}'
def full_number_func(self):
return f'{self.number_code}|{self.phone_no}'
class Meta:
abstract = True
class Customer(allmodelfields):
account_type = models.CharField(max_length=20,choices=customer_account_choices,default=" ")
class Dealer(allmodelfields):
account_type = models.CharField(max_length=20,choices= dealer_account_choices,default=" ")
serializers.py
class customerprofileserializer(serializers.ModelSerializer):
class Meta:
model = Customer
fields = ['id','full_name','email','unique_id','full_number']
class dealerprofileserializer(serializers.ModelSerializer):
class Meta:
model = Dealer
fields = ['id','full_name','email','unique_id','full_number']
urls.py
path("customerprofileview/", customerprofileview.as_view(),name='customerprofileview'),
path("dealerprofileview/", dealerprofileview.as_view(),name='dealerprofileview'),
views.py
class customerloginview(APIView):
renderer_classes = [apirenderer]
def post(self,request,format=None):
serializer_object = loginserializer(data = request.data)
if serializer_object.is_valid():
email = serializer_object.initial_data["email"].lower()
password= serializer_object.initial_data["password"]
is_email_exists = Customer.objects.filter(email=email).exists()
if is_email_exists:
user = Customer.objects.get(email=email)
if user.is_active:
valid_password = user.check_password(password)
if valid_password:
#token = get_tokens_for_user(user)
refresh = RefreshToken.for_user(user)
return Response({'msg':f"{user.full_name} you are logged in successfully !",'tokens':{'access':str(refresh.access_token),'refresh':str(refresh)}},status=status.HTTP_200_OK)
else:
return Response({'msg':'Incorrect password'})
else:
# resend user activation
current_site = get_current_site(request)
mail_subject = 'Please ACTIVATE your ACCOUNT (Resent)'
message = render_to_string('account_verification_email.html',{
'last_user':user,
'domain' : current_site,
'id':(user.unique_id)[:4],
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': default_token_generator.make_token(user),
})
to_email = user.email
send_email = EmailMessage(mail_subject,message,to=[to_email])
send_email.send()
return Response({'msg':f'your account is not activated yet.we have resent an account activation link on {user.email} . please activate then login'})
else:
return Response({'msg':'Email does not exist.'})
else:
return Response(serializer_object.errors,status = status.HTTP_400_BAD_REQUEST)
class customerprofileview(APIView):
renderer_classes = [apirenderer]
permission_classes = [IsAuthenticated]
def get(self,request,format=None):
serializer_object = customerprofileserializer(request.user)
return Response(serializer_object.data,status=status.HTTP_200_OK)
#same for dealer
settings.py
SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=5),
'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
'ROTATE_REFRESH_TOKENS': False,
'BLACKLIST_AFTER_ROTATION': False,
'UPDATE_LAST_LOGIN': False,
'ALGORITHM': 'HS256',
'SIGNING_KEY': SECRET_KEY,
'VERIFYING_KEY': None,
'AUTH_HEADER_TYPES': ('Bearer',),
'AUTH_HEADER_NAME': 'HTTP_AUTHORIZATION',
'USER_ID_FIELD': 'id',
'USER_ID_CLAIM': 'user_id',
'USER_AUTHENTICATION_RULE': 'rest_framework_simplejwt.authentication.default_user_authentication_rule',
'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',),
'TOKEN_TYPE_CLAIM': 'token_type',
'TOKEN_USER_CLASS': 'rest_framework_simplejwt.models.TokenUser',
'JTI_CLAIM': 'jti',
}
我已经搜索了很多,但没有得到任何解决方案。我想通过生成的访问令牌获得登录用户的详细信息,如全名,号码,身份证等。
我在youtube上看到过一个人做同样的事情。
1条答案
按热度按时间8cdiaqws1#
我也遇到过类似的问题,我知道这是因为设置。检查您的JWT默认验证类和AUTH_USER_MODEL。您正在处理3个模型。因此您需要覆盖一些使用默认用户模型的函数。一切顺利