Hello I am learning to make api with django I am in the login part but for some reason when I try to login and the token is generated it gives me this error how can I solve it
Internal Server Error: /gateway/login Traceback (most recent call last): File "D:\Users\ferna\Documents\Cursos\youtube\djangoRest\django_api\env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner response = get_response(request) File "D:\Users\ferna\Documents\Cursos\youtube\djangoRest\django_api\env\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "D:\Users\ferna\Documents\Cursos\youtube\djangoRest\django_api\env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "D:\Users\ferna\Documents\Cursos\youtube\djangoRest\django_api\env\lib\site-packages\django\views\generic\base.py", line 70, in view return self.dispatch(request, *args, **kwargs) File "D:\Users\ferna\Documents\Cursos\youtube\djangoRest\django_api\env\lib\site-packages\rest_framework\views.py", line 509, in dispatch response = self.handle_exception(exc) File "D:\Users\ferna\Documents\Cursos\youtube\djangoRest\django_api\env\lib\site-packages\rest_framework\views.py", line 469, in handle_exception self.raise_uncaught_exception(exc) File "D:\Users\ferna\Documents\Cursos\youtube\djangoRest\django_api\env\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception raise exc File "D:\Users\ferna\Documents\Cursos\youtube\djangoRest\django_api\env\lib\site-packages\rest_framework\views.py", line 506, in dispatch response = handler(request, *args, **kwargs) File "D:\Users\ferna\Documents\Cursos\youtube\djangoRest\django_api\gateway\views.py", line 51, in post Jwt.objects.create( File "D:\Users\ferna\Documents\Cursos\youtube\djangoRest\django_api\env\lib\site-packages\django\db\models\manager.py", line 85, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "D:\Users\ferna\Documents\Cursos\youtube\djangoRest\django_api\env\lib\site-packages\django\db\models\query.py", line 451, in create obj = self.model(**kwargs) File "D:\Users\ferna\Documents\Cursos\youtube\djangoRest\django_api\env\lib\site-packages\django\db\models\base.py", line 485, in init _setattr(self, field.name, rel_obj) File "D:\Users\ferna\Documents\Cursos\youtube\djangoRest\django_api\env\lib\site-packages\django\db\models\fields\related_descriptors.py", line 215, in set
raise ValueError( ValueError: Cannot assign "7": "Jwt.user_id" must be a "CustomUser" instance. [04/May/2021 22:00:06] ←[35;1m"POST /gateway/login HTTP/1.1" 500 127750←[0m
it's code from model
class Jwt(models.Model): user_id = models.ForeignKey(CustomUser, related_name='login_user', on_delete=models.CASCADE) access = models.TextField() refresh = models.TextField() created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True)
and this is from view
def get_random(length): return ''.join(random.choices(string.ascii_uppercase + string.digits, k=length)) def get_access_token(payload): return jwt.encode( {"exp": datetime.now() + timedelta(minutes=5), **payload}, settings.SECRET_KEY, algorithm="HS256" ) def get_refresh_token(): return jwt.encode( {"exp": datetime.now() + timedelta(days=365), "data": get_random(10)}, settings.SECRET_KEY, algorithm="HS256" ) class LoginView(APIView): serializer_class = LoginSerializer def post(self, request): serializer = self.serializer_class(data=request.data) serializer.is_valid(raise_exception=True) user = authenticate( username=serializer.validated_data['email'], password=serializer.validated_data['password']) if not user: return Response({"error": "Invalid email or password"}, status="400") access = get_access_token({"user_id": user.id}) refresh = get_refresh_token() Jwt.objects.create( user_id=user.id, access=access, refresh=refresh ) return Response({"access": access, "refresh": refresh}) class RegisterView(APIView): serializer_class = RegisterSerializer def post(self, request): serializer = self.serializer_class(data=request.data) serializer.is_valid(raise_exception=True) CustomUser.objects._create_user(**serializer.validated_data) return Response({"success": "User created."})
没有评论:
发表评论