如何在将密码保存到用户模型 Django 之前对其进行加密?

How to Encrypt Password before saving it to User Model Django?

我创建了一个用户注册页面,它将用户添加到 Django 中的用户模型

但是保存数据时,密码没有加密,即只存储为文本。 这会在用户尝试登录时产生问题(因为 Django 试图解密原始密码,但它们不匹配)。

此外,我正在扩展用户模型,以便添加更多关于保存在配置文件模型中的用户的信息(使用一对一 Link)

views.py

def user_signup(request):
    if request.method == "POST":
        user_form = userSignup(request.POST)
        phone = request.POST['phone']
        address = request.POST['address']
        pincode = request.POST['pincode']
        
        if user_form.is_valid() :
            user = user_form.save()
            auth.login(request,user)
            userdata = User.objects.all()
            for userinfo in userdata:
                if userinfo.username == user.username:
                    user_id=user.id
            update_data = Profile.objects.get(pk = user_id)
            update_data.address=address
            update_data.phone=phone
            update_data.pincode=pincode
            update_data.save()
            return redirect('/')

        else:
            return HttpResponse(" SIGNUP FAILED")

    else:
        form = userSignup()
        profile_form = userSignup_profile()
        return render(request,"user_signup.html",{'form':form, 'profile_form':profile_form})


def user_logout(request):
    auth.logout(request)
    return redirect('/')

user_signup.html

<body>
    <form action="user_signup" method="POST"> 
        {% csrf_token %}
        {{form.as_p}}
        {{ profile_form.as_p}}
        <button class="primary" type="submit" >SIGNUP </button>
    </form>
</body>

Models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    phone = models.CharField(max_length=10)
    address = models.TextField(max_length=200)
    pincode = models.IntegerField()

forms.py

class userSignup(forms.ModelForm):
    password = forms.CharField(max_length=50, widget = forms.PasswordInput())

    class Meta:
        model = User
        fields = ('first_name', 'last_name','username', 'password',  'email')

如何以加密形式保存新用户密码,而不是实际密码?

# You have to import make_password

from django.contrib.auth.hashers import make_password

# you have to pass string as parameter
password = "123"
make_password(password)



# You can write your code like this:-


def user_signup(request):
    if request.method == "POST":
        user_form = userSignup(request.POST)
        phone = request.POST['phone']
        address = request.POST['address']
        pincode = request.POST['pincode']

        if user_form.is_valid() :
            user = user_form.save(commit=False)
            user.password = make_password("123")
            user.save()
            .......
            .......

Django make_password (source code) 函数将纯文本密码转换为适合存储在持久数据库中的散列。

您绝对不想尝试使用自己的加密和散列函数来存储密码,因为此功能已经存在。

只需将您的 views.py 编辑为:

from django.contrib.auth.hashers import make_password

def user_signup(request):
    if request.method == "POST":
        user_form = userSignup(request.POST)
        phone = request.POST['phone']
        address = request.POST['address']
        pincode = request.POST['pincode']

        if user_form.is_valid() :
            # Hash password using make_password() function
            user = user_form.save(commit=False)
            user.password = make_password(user.password)
            user.save()

            ...
import crypt
# To encrypt the password. This creates a password hash with a random salt.
password_hash = crypt.crypt(password)

# To check the password.
valid_password = crypt.crypt(cleartext, password_hash) == password_hash

来源:https://docs.djangoproject.com/en/1.8/topics/auth/passwords/#django.contrib.auth.hashers.make_password