[英]“Invalid password format or unknown hashing algorithm.” Django - new user
I am trying to create a new user for my application by following this tutorial .我正在尝试按照本教程为我的应用程序创建一个新用户。 I am able to create a new user but the on opening django admin page the password is shown as
我可以创建一个新用户,但是在打开 django 管理页面时,密码显示为
"Invalid password format or unknown hashing algorithm."
“无效的密码格式或未知的散列算法。”
I have seen similar questions where the answer was to set我见过类似的问题,答案是设置
account.set_password(password)
account.set_password(密码)
I have tried this but it did not work for me.我已经尝试过了,但它对我不起作用。
(EDIT: It seems the.save() is not being called from views.py. I am not sure why) (编辑:似乎没有从views.py调用.save()。我不知道为什么)
Here is my code这是我的代码
Serializer.py序列化程序.py
class RegistraionSerialzer(serializers.ModelSerializer):
password2 = serializers.CharField(style={'input_type':'password'},read_only=True)
class Meta:
model = User
fields = ('phone','name','password','password2','user_type','email')
extra_kwargs = {
'password': {'write_only' : True},
}
def save(self):
print('here')
account = User(
phone = self.validated_data['phone'],
email = self.validated_data['email']
)
password = self.validated_data['password']
password2 = self.validated_data['password2']
if password != password2:
print(password)
return serializers.ValidationError({'password':'password mismatch'})
print(password)
account.set_password(password)
account.save()
Views.py视图.py
@api_view(['POST'])
def Registraion(request):
serializer = RegistraionSerialzer(data=request.data)
data = {}
print('reg vie')
if serializer.is_valid():
account = serializer.save()
data['response'] = "Successfully registered "
data['name'] = account.name
data['password'] = account.password
print(data)
else:
print(serializer.errors)
return Response(data)
try this尝试这个
from django.contrib.auth.hashers import make_password
class RegistraionSerialzer(serializers.ModelSerializer):
password2 = serializers.CharField(style={'input_type':'password'},read_only=True)
class Meta:
model = User
fields = ('phone','name','password','password2','user_type','email')
extra_kwargs = {
'password': {'write_only' : True},
}
def save(self):
print('here')
account = User(
phone = self.validated_data['phone'],
email = self.validated_data['email']
)
password = self.validated_data['password']
password2 = self.validated_data['password2']
if password != password2:
print(password)
return serializers.ValidationError({'password':'password mismatch'})
print(password)
account.set_password(make_password(password))
account.save()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.