不知道如何散列passowrd django

不知道如何散列passowrd django,django,django-views,Django,Django Views,我正在做一个项目,我有一个表单,用户在其中输入用户名电子邮件和密码。我想散列密码并保存它。我注意到,当我通过admin页面创建用户时,它会自动创建密码并在保存之前对其进行哈希处理。我也想做同样的事情。我有没有办法做到这一点 这就是我现在看到的 def signup(request): # the following will determine if the form is submitted or not if request.method == 'POST':

我正在做一个项目,我有一个表单,用户在其中输入用户名电子邮件和密码。我想散列密码并保存它。我注意到,当我通过admin页面创建用户时,它会自动创建密码并在保存之前对其进行哈希处理。我也想做同样的事情。我有没有办法做到这一点

这就是我现在看到的

def signup(request):
    # the following will determine if the form is submitted or not
    if request.method == 'POST':
        form = SignupForm(request.POST)
        # the following section validates the entire form and processed the data
        if form.is_valid():
            # the following will make sure the data is clean and then store them
            # into new variables
            cd = form.cleaned_data
            username = cd['username']
            password = cd['password']
            verify = cd['verify']
            email = cd['email']
            # the folloiwng will make sure the password and verification are matching
            # before storing the info into the database
            if password == verify:
                new_user = User.objects.create(
                    username = username,
                    password = password,
                    email = email,
                )
                # the following will store the username of the account that was just
                # created in to the session so that the app can track the user that
                # is logged in
                request.session['username'] = username
                return redirect('profile_setup')
            else:
                # if password and verification dont match, a message will be sent
                # back to the user so they can fill in the correct info.
                message = 'Password and Verify dont match'
                parameters = {
                    'form':form,
                    'message':message,
                }
                return render(request, 'tabs/signup.html', parameters)
    else:
        # this will display the form if it waas not submmited.
        form = SignupForm()
        message = 'Fill out the form'
        parameters = {
            'form':form,
            'message':message,
        }
        return render(request, 'tabs/signup.html', parameters)

希望这就是您要查找的内容

有关详细信息,请参阅django官方文档

from django.contrib.auth.hashers import make_password

if password == verify:
                new_user = User.objects.create(
                    username = username,
                    password = make_password(password),
                    email = email,
                )
if password == verify:
    new_user = User.objects.create(
        username = username,
        email = email,
    )
    new_user.set_password(password)
    new_user.save()