简体   繁体   中英

Is there a way to encrypt the password when creating a non-admin user in Django Admin Panel?

I'm new with Django and I making an app just for practice so I wanted to try to create different users, so I have the admin user and other 2. I'm not making a sing up page as I wanted to only be able to create the users in the admin panel so I have:

#models.py

from django.db import models

# Create your models here.
class UserType1(models.Model):
    Username = models.CharField(max_length=50, null=False)
    Password = models.CharField(max_length=50, null=False)

    class Meta:
        ordering = ('Username',)

    def __str__(self):
        return self.Username

class NormalUser(models.Model):
    Username = models.CharField(max_length=50, null=False)
    Password = models.CharField(max_length=50, null=False)

    class Meta:
        ordering = ('Username',)

    def __str__(self):
        return self.Username

And to show it in the Admin panel:

#admin.py

from django.contrib import admin
from . import models

# Register your models here.
@admin.register(models.UserType1)
class UserType1Admin(admin.ModelAdmin):
    list_display = ('Username','Password')

    search_fields = ("Username", )


@admin.register(models.NormalUser)
class NormalUserAdmin(admin.ModelAdmin):
    list_display = ('Username','Password')

    search_fields = ("Username", )

For example, creating a NormalUser, as you can see, that's how it's saved, the password it's just simple text:

Normal User Creation:

普通用户创建

Showing Normal User:

显示普通用户

In the future, they will have different fields but, for now, the thing I want to know if there is a way to save the password encrypted in the data base as is done with the admin user. Is there a simple way to save the password encrypted?

PS: If something it's not clear, feel free to ask me anything.

Check this out!

https://docs.djangoproject.com/en/3.2/topics/auth/passwords/

Password management in Django

Password management is something that should generally not be reinvented unnecessarily, and Django endeavours to provide a secure and flexible set of tools for managing user passwords. This document describes how Django stores password, how the storage hashing can be configured, and some utilities to work with hashed passwords.

Django provides a flexible password storage system and uses PBKDF2 by default.

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