Django project practice (mall): two, user registration (preparation)

Insert picture description here

(According to the content of teacher's live broadcast)

1. User registration page preparation

1. Create APP users

2. Define the register method

3. Register the users app application

4. Register the register route

Two, Django default user authentication system

1. Django comes with a user authentication system

  • It handles user accounts, groups, permissions, and cookie-based user sessions.

2. Django authentication system location

  • django.contrib.auth contains the core of the authentication framework and the default model.
  • django.contrib.contenttypes is the Django content type system, which allows permissions to be associated with the models you create.

3. The Django authentication system handles authentication and authorization at the same time

  • Authentication: Verifies whether a user is who he claims to be, and can be used for account login.
  • Authorization: Authorization determines what an authenticated user is allowed to do.

4. What's included in the Django authentication system

  • User: User model class, user authentication.
  • Permission: Identifies whether a user can do a specific task, commonly used in MIS systems.
  • Group: Unified management of multiple users with the same authority, commonly used in MIS systems.
  • Password: A configurable password hashing system, setting passwords and password verification.

Three, Django default user model class: AbstractUser

1. Basic attributes of Django default User object:

  • Required to create a user: username, password
  • Create user optional: email, first_name, last_name, last_login, date_joined, is_active, is_staff, is_superuse
  • Determine whether the user is authenticated: is_authenticated

Four, custom user model class

  • Custom user model class: AbstractUser attribute + custom attribute

1. Define a custom user model

# ./apps/users/models.py
from django.db import models
from django.contrib.auth.models import AbstractUser


# 重写用户模型类, 继承自 AbstractUser
class User(AbstractUser):
    """自定义用户模型类"""
    # 在用户模型类中增加 mobile 字段
    mobile = models.CharField(max_length=11, unique=True, verbose_name='手机号')

    # 对当前表进行相关设置:
    class Meta:
        db_table = 'tb_users'
        verbose_name = '用户'
        verbose_name_plural = verbose_name

    # 在 str 魔法方法中, 返回用户名称
    def __str__(self):
        return self.username

2. Specify the user model class

  • The Django user model class is determined by the global configuration item AUTH_USER_MODEL
    • Django's global variables are stored in conf.global_settings.py
      Insert picture description here
  • Because we have rewritten the user model class, we need to re-specify the default user model class:
    add the following code in the dev.py file:
./lgshop/dev.py

# 指定自定义的用户名 app.模型
AUTH_USER_MODEL = 'users.User'

3. Database migration

Insert picture description here

python manage.py makemigrations
python manage.py migrate

Insert picture description here

Guess you like

Origin blog.csdn.net/laoluobo76/article/details/113007330
Recommended