简体   繁体   中英

Django:How password store in django auth_user table

I am customizing the django user module and adding some extra fields

my model is looking like

class Drinker(models.Model):
    user = models.OneToOneField(User)
    birthday = models.DateField()
    name     = models.CharField(max_length = 100)

    def __unicode__(self):
        return self.name

And here is my register view

def DrinkerRegistration(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('/profile/')
    if request.method == "POST":
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = User.objects.create(username = form.cleaned_data['username'],email = form.cleaned_data['email'],password = form.cleaned_data['password'])
            user.save()
            drinker = Drinker(name = form.cleaned_data['name'],birthday = form.cleaned_data['birthday'],user=user)
            drinker.save()
            return HttpResponseRedirect('/profile/')
    else:
        form = RegistrationForm()
        context = {'form':form}
        return render_to_response('register.html',context,context_instance = RequestContext(request))

Now my question is when the user is registering in my auth_user table the password is storing in plan text ie causing

Unknown password hashing algorithm '123'. Did you specify it in the PASSWORD_HASHERS setting?

error at the time of login

but for superusers passwords are in SHA1 format (i am not sure ) in auth_user table and they are able to login

please suggest me how to hash the password field here?

不要使用User.objects.create() ,使用User.objects.create_user() - 它将使用正确的算法散列提供的密码。

You should let User.set_password hash the password and store it in the appropriate format, or use the appropriate manager function as mentioned in the other answer.

Basically you never asked Django to hash the password and tried to put the plain text in your database. In doing so, the format (hasher salt hashed_pass) that Django expected was not met hence the error.

Better yet, those parts of your app are best done using reusable apps such as Django Registration.

You can then use signals to create the Drinker profile after an user registers (is created).

您需要使用create_user()帮助程序函数,该函数将正确设置密码。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM