【问题标题】:"Invalid password format or unknown hashing algorithm." Django - new user“无效的密码格式或未知的散列算法。” Django - 新用户
【发布时间】:2021-03-27 17:46:18
【问题描述】:

我正在尝试通过关注 this tutorial 为我的应用程序创建一个新用户。我可以创建一个新用户,但是在打开 django 管理页面时,密码显示为

“无效的密码格式或未知的哈希算法。”

我见过类似的问题,答案是设置

account.set_password(密码)

我已经尝试过了,但它对我不起作用。

(编辑:似乎没有从views.py调用.save()。我不知道为什么)

这是我的代码

Serializer.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

@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)

【问题讨论】:

  • 您已注册的用户仍将包含非散列密码。因此,您应该确保所有您在其中创建用户或更新密码的视图使用.set_password,并确保使用新密码删除/更新旧用户。
  • @WillemVanOnsem 目前这是创建用户的唯一视图。我尝试通过终端手动添加一个用户,并且该用户的密码具有正确的散列
  • 问题是:您是否在 更改逻辑之前创建了用户,仍然有使用 逻辑生成的密码。
  • 只是为了确定......您的代码中有一些错误(如self.self,并调用account.create_user(),这是标准Django用户上不存在的方法)。您是否检查过您的注册请求是否有效?
  • @WillemVanOnsem 我删除了数据库和迁移,并使用当前逻辑重新开始,但仍然面临同样的问题

标签: python django authentication django-rest-framework


【解决方案1】:

试试这个

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()

【讨论】:

    猜你喜欢
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 2020-05-25
    • 2020-11-27
    • 2014-07-06
    • 2013-01-09
    • 2015-06-02
    相关资源
    最近更新 更多