'django authentication check.password doesn't work
check.password doesn't work and this is the error message :
packages\django\contrib\auth\base_user.py", line 115, in check_password return check_password(raw_password, self.password, setter) File "C:\Python310\lib\site-
packages\django\contrib\auth\hashers.py", line 55, in check_password must_update = hasher_changed or preferred.must_update(encoded) File "C:\Python310\lib\site-
packages\django\contrib\auth\hashers.py", line 332, in must_update decoded = self.decode(encoded) File "C:\Python310\lib\site-
packages\django\contrib\auth\hashers.py", line 308, in decode algorithm, iterations, salt, hash = encoded.split("$", 3) ValueError: not enough values to unpack (expected 4, got 3)
this is the views:
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.exceptions import AuthenticationFailed
from .serializers import UserSerializer
from .models import User
class RegisterView(APIView):
def post(self, request):
serializer = UserSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data)
class LoginView(APIView):
def post(self, request):
email = request.data['email']
password = request.data['password']
user = User.objects.filter(email=email).first()
if user is None:
raise AuthenticationFailed('user not found')
if not user.check_password(password):
raise AuthenticationFailed('incorrect password !')
return Response({
'message':'succes'})
the serializers :
from rest_framework import serializers
from .models import User
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields =['id','name','email','password']
extra_kwargs = {
'password':{'write_only':True}
}
def create(self, validated_data):
password = validated_data.pop('password',None)
instance = self.Meta.model(**validated_data)
if password is not None:
instance.set_password(password)
instance.save()
return instance
the models :
import email
from unicodedata import name
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here
class User(AbstractUser):
name = models.CharField(max_length=50)
email = models.CharField(max_length=50 , unique=True)
password = models.CharField(max_length=30)
username = None
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
