简体   繁体   English

基于 Django 类的视图中无效的密码格式或未知的散列算法

[英]Invalid password format or unknown hashing algorithm in Django class based view

I want to create user by API view.我想通过 API 视图创建用户。 But I get this problem:但我遇到了这个问题:

在此处输入图片说明

serializers.py序列化程序.py

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('username', 'password')

views.py视图.py

class UserRegistration(CreateAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

How can I solve this problem?我怎么解决这个问题?

You should override serializer's create() method to hash password before saving new user object.在保存新用户对象之前,您应该覆盖序列化程序的create()方法来散列密码。 You can use set_password for this:您可以为此使用set_password

 class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('username', 'password')

    def create(self, validated_data):
        user = User(
            username=validated_data['username']
        )
        user.set_password(validated_data['password'])
        user.save()
        return user

Otherwise User.password will be stared in DB without hashing which is not secure.否则 User.password 将在没有散列的情况下盯着数据库,这是不安全的。

Also you can use create_user method which is calling set_password by defaut:您也可以使用create_user方法,该方法默认调用set_password

 class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('username', 'password')

    def create(self, validated_data):
        return User.objects.create_user(**validated_data)

try this尝试这个

from django.contrib.auth.hashers import make_password

class UserSerializer(serializers.ModelSerializer):
class Meta:
    model = User
    fields = ('username', 'password')

def create(self, validated_data):
    user = User(
        username=validated_data['username']
    )
    user.set_password(make_password(validated_data['password']))
    user.save()
    return user

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “无效的密码格式或未知的散列算法。” Django - 新用户 - “Invalid password format or unknown hashing algorithm.” Django - new user 如何修复 Django 中“无效的密码格式或未知的哈希算法”错误? - How to fix “invalid password format or unknown hashing algorithm” error in Django? 无效的密码格式或未知的散列算法。 在 django 休息框架中 - Invalid password format or unknown hashing algorithm. in django rest-framework 由于“密码格式无效或哈希算法未知”,用户无法登录。-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 密码:即使在创建用户功能中设置后,密码格式仍然无效或哈希算法未知 - Password: Invalid password format or unknown hashing algorithm even after setting in the create user function 使用密码哈希构建密码算法 - Building a password algorithm, with password hashing 未知的密码哈希算法。 PASSWORD_HASHERS 设置 - 使用工厂男孩 - Unknown password hashing algorithm. PASSWORD_HASHERS setting - using factory boy 基于 Django class 的 View with 2 post forms 返回有效未知 - Django class based View with 2 post forms returns valid unknown django中基于类的视图 - class based view in django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM