Django Authentication – How to build Login/Logout/Signup for custom User
Django
11
authentication
20
Male avatar

loveprogramming viết ngày 20/04/2021

https://grokonez.com/django/django-authentication-login-example-logout-signup-for-custom-user-tutorial

Django Authentication – How to build Login/Logout/Signup for custom User

Building user authentication is not easy, in almost case, it's complicated. Fortunately, Django has a powerful built-in User authentication that helps us create our Authentication system fast. By default, the User model in Django auth app contains fields: username, password, email, first_name, last_name... However, using our own custom user model allows us deal with user profile more comfortably. For example, what if we want to add more fields: full_name or age?

In this tutorial, we're gonna look at way to customize authentication in Django (version 2.1) using subclass of AbstractBaseUser: AbstractUser. All User authentication data will be stored in MySQL/PostgreSQL database that we'll show you how to config the datasource.

It will be very interesting. Let's go through the steps.

Django Custom Authentication Project overview

Goal

We will build a Dajngo Project with Authentication app that has login/logout/signup with custom fields such as full name and age:

django-authentication-example-signup-login-logout-project-goal

We will code our custom signup() function, login() and logout() is automatically implemented by Django auth.

All User data will be saved in MySQL/PostgreSQL database.

Project Structure

Here is the folders and files structure that we will create in the next steps.

django-authentication-example-signup-login-logout-project-structure

Setup Django Custom Authentication Project

Create Django project named DjangoAuth with command:
django-admin startproject DjangoAuth

Run following commands to create new Django App named authen inside the project:

  • cd DjangoAuth
  • python manage.py startapp authen

Open upload/apps.py, we can see AuthenConfig class (subclass of the django.apps.AppConfig) that represents our Django app and its configuration:


from django.apps import AppConfig


class AuthenConfig(AppConfig):
    name = 'authen'

Open settings.py, find INSTALLED_APPS, then add:


INSTALLED_APPS = [
    ...
    'authen.apps.AuthenConfig',
]

Config Django project to work with database

MySQL Database

Install & Import Python MySQL Client

We have to install Python MySQL Client to work with MySQL database.
In this tutorial, we use pymysql: pip install pymysql.

Once the installation is successful, import this module in DjangoAuth/init.py:

https://grokonez.com/django/django-authentication-login-example-logout-signup-for-custom-user-tutorial

Django Authentication – How to build Login/Logout/Signup for custom User

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

451 bài viết.
79 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 6 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 5 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 5 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/django/djangorestapispostgresqlexampledjangorestframeworktutorial In this tutorial, we’re gonna look at way to create Django ...
loveprogramming viết 1 tháng trước
0 0
Male avatar
0 0
https://ozenero.com/djangoangular6exampledjangorestframeworkmysqlcrudexamplepart2djangoserver Django + Angular 6 example | Django Rest Framework +...
loveprogramming viết 5 ngày trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
451 bài viết.
79 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!