User module creation for python framework Django real-world mall project

Create User APP

There will be multiple applications throughout the project and they need to be stored in a separate file package, so create a new apps directory to manage all the sub-applications.

Wear-through users application under apps package directory

 python ../../ startapp users

At this time, we need to register the newly created application into django, but here we have modified the application's management directory, which is different from the default way. If you also register the APP in the previous way, you will surely get an error. At this time, we can first check the django's package path and enter it in the dev file.

print(sys.path()) # All search paths for output packages

The first path is the home directory of our django project.

That is, he will search all the packages in the home directory, so he can define the path of the APP as


It is possible to run the program successfully at this time.However, it's too cumbersome to define how to register an APP in this way. If you use it a lot, you should write it this way every time. That's not boring.So it has to be simplified.How can you simplify this by inserting an absolute path to the apps directory directly into the package's search path, so that django can search for it?

sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))

That's it, then sign up for APP

    # 'immortal_mall.apps.users',

Return to the registration page

Prepare to register templates for use in a pre-created Templates folder

Define the user registration view class:

class RegisterView(View):
    """User Registration View Class"""

    def get(self, request):
        '''Get the registration page'''
        return render(request, 'register.html')

Define User Registration Routes

# Total Route
urlpatterns = [
    path('', include(('users.urls', 'users'), namespace='users'))

It is important to note that the first parameter of the include function is a meta-ancestor. The first parameter, not to mention, specifies the route of the sub-application. The second parameter, app_name, must be specified here. If this parameter is not specified, writing include('users.urls', namespace='users') will fail.

Another way to specify this is to specify app_name='users'in the child application's urls file. Create a new file in the users application directory and write the routing information

urlpatterns = [
    path('register/', views.RegisterView.as_view(), name='register')  # name Add Namespace

Launch application, browser request Http://, return to the registration page.

User Model Class

The project uses the user authentication system that comes with django. First, let's see what functions are available.

Django Default User Authentication System

The Django built-in user authentication system handles user accounts, groups, permissions, and cookie-Based user sessions at django.contrib.auth

In the package.

The auth package is an APP built into django that, like admin, handles both authentication and authorization. Authentication is the person who verifies whether a user is a system person, and authorization determines what an authenticated user can be allowed to do.

The user model class User is provided in the Django authentication system to store user's data. The User object is the core of the authentication system:

class User(AbstractUser):
    Users within the Django authentication system are represented by this

    Username and password are required. Other fields are optional.
    class Meta(AbstractUser.Meta):
        swappable = 'AUTH_USER_MODEL'

The Userl class is nothing. Look at what's in the parent AbstractUser, which defines some fields of the user, including some required fields of the user class, username, password, and other unnecessary fields, is_active,is_staff, etc. The methods for user authentication are all in AbstractBaseUser, the parent of AbstractUser.

However, instances of UserManager held in the AbstractUser class are called objects, which provide methods for creating users, such as:

user = User.objects.create_user(username, email, password, **extra_fields)

Custom User Model Class

This is a user registration information form with a field for mobile number, but Django provides a field that is not available in the user model and needs to be defined by ourselves.

class User(AbstractUser):
    """Custom User Model Class"""
    mobile = models.CharField(max_length=11, unique=True, verbose_name="Cell-phone number")

    class Meta:
        db_table = 'tb_user' # Custom Table Name
        verbose_name = "user"  # Site Display
        verbose_name_plural = verbose_name # Complex Display

Custom user model classes need to inherit the AbstractUser class and specify the newly added fields.Running the project after adding will result in an error:

This is the default authentication object configuration for the django system. We used a custom object, but this object was not assigned to the system.

So re-specify it in your dev configuration file;

AUTH_USER_MODEL = 'users.User'

Then create the migration file, execute the migration command, and complete the table creation.

✗ python makemigrations

 python migrate

Welcome to My Blog Look, there's more about testing the real world!!

9 original articles published. 0% praised. 220 visits
Private letter follow

Tags: Django Python Mobile Pycharm

Posted on Mon, 09 Mar 2020 23:54:49 -0400 by ruthsimon