简体   繁体   中英

django update view password and ForeignKey

i currently use django update/create view and i have some problems:

  1. how can i update/create a password? - i can show the old password but

it doesn't save the new one with the django hash algorithm so the password is

ignored and the user cant log in anymore.

class Update(UpdateView):
    model = User
    fields = ['username', 'password']
  1. how can i update/create a Foreign Key?

  2. is there a way to custom the fields? ie to show them as

radio/checkbox/password?

thx

I can show the old password but it doesn't save the new one with the django hash algorithm so the password is ignored and the user cant log in anymore.

That's because for security, Django doesn't store raw passwords, it stores a hash of the raw password, which is sufficient to tell if a user entered the correct password

To set the password use User.set_password()

user = request.user # or another user source
user.set_password('raw password string')

So instead of changing the field directly, change the password like above to store the hash (not the raw password), and don't bother with "showing old password", a secure system won't be able to

https://docs.djangoproject.com/en/1.8/ref/contrib/auth/#django.contrib.auth.models.User.set_password

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