简体   繁体   English

“无效的密码格式或未知的散列算法。” Django - 新用户

[英]“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.

相关问题 由于“密码格式无效或哈希算法未知”,用户无法登录。-Django - User can't login because of "Invalid password format or unknown hashing algorithm."-Django 如何修复“无效的密码格式或未知的哈希算法”。 在自定义用户中 Model Django - How to fix "Invalid password format or unknown hashing algorithm." in a custom User Model Django 无效的密码格式或未知的散列算法。 在 django 休息框架中 - Invalid password format or unknown hashing algorithm. in django rest-framework 如何修复 Django 中“无效的密码格式或未知的哈希算法”错误? - How to fix “invalid password format or unknown hashing algorithm” error in Django? 基于 Django 类的视图中无效的密码格式或未知的散列算法 - Invalid password format or unknown hashing algorithm in Django class based view 密码:即使在创建用户功能中设置后,密码格式仍然无效或哈希算法未知 - Password: Invalid password format or unknown hashing algorithm even after setting in the create user function 未知的密码哈希算法。 PASSWORD_HASHERS 设置 - 使用工厂男孩 - Unknown password hashing algorithm. PASSWORD_HASHERS setting - using factory boy 使用密码哈希构建密码算法 - Building a password algorithm, with password hashing 用户登录系统未哈希并比较密码与记录Django - User login system is not hashing and comparing password with record Django Django Admin中的密码小部件和密码哈希 - Password Widget and Password Hashing in Django Admin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM