Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Django implements built-in auth authentication system

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "Django to achieve the built-in auth authentication system", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Django to achieve the built-in auth authentication system" it!

Auth module from django.contrib import authauthenticate () method

Provide user authentication function, that is, verify whether the user name and password are correct, generally need username, password two keyword parameters. If the authentication is successful (the user name and password are correct and valid), a User object is returned.

Usage:

User = auth.authenticate (request,username='theuser',password='thepassword') login (HttpRequest, user) method

This function accepts a HttpRequest object and an authenticated User object.

This function implements a user login function. It essentially generates relevant session data for the user on the back end.

Usage:

From django.contrib.auth import authenticate, logindef my_view (request): username= request.POST ['username'] password= request.POST [' password'] user = authenticate (request, username=username, password=password) if user is not None: login (request, user) # Redirect to a success page. ... Else: # Return an 'invalid login' error message. ... logout (request) method

This function accepts a HttpRequest object with no return value.

When this function is called, all the currently requested session information is cleared. Even if the user is not logged in, using this function will not report an error.

Usage:

From django.contrib.auth import logoutdef logout_view (request): logout (request) # Redirect to a success page.is_authenticated () method

It is used to determine whether the current request has been authenticated.

Usage:

Def my_view (request): if not request.user.is_authenticated (): return redirect ('% sprinter% s'% (settings.LOGIN_URL, request.path)) login_requierd () method

Auth provides us with a decorator tool that is used to quickly add login checks to a view.

From django.contrib.auth.decorators import login_required@login_requireddef my_view (request):...

If the user is not logged in, he or she will jump to django's default login URL / accounts/login/ and pass the absolute path of the current access to url (which will be redirected after successful login). If you need to customize the URL for login, you need to modify it through LOGIN_URL in the settings.py file. LOGIN_URL ='/ login/'

Create_user () method

A method for creating new users provided by auth, which needs to provide the necessary parameters (username, password), etc.

Usage:

From django.contrib.auth.models import Useruser = User.objects.create_user (username=' username', password=' password', email=' mailbox',...) create_superuser () method

Auth provides a way to create a new superuser, which requires providing the necessary parameters (username, password), and so on.

Usage:

From django.contrib.auth.models import Useruser = User.objects.create_superuser (username=' username', password=' password', email=' mailbox',...) check_password (password) method

A method provided by auth to check whether the password is correct needs to provide the password of the currently requesting user. The password returns True correctly, otherwise False is returned.

Ok = user.check_password ('password') set_password (password) method

A method provided by auth to change the password, which receives the new password to be set as a parameter.

Usage:

User.set_password (password='') user.save ()

Note: be sure to call the save method of the user object after setting up!

Properties of the User object

User object properties: username, password

Is_staff: whether the user has administrative rights to the website.

Is_active: whether to allow users to log in, set to False, you can disable users from logging in without deleting users.

Extend the default auth_ user table

You can define your own AbstractUser class by inheriting the built-in Model class. In this way, we can not only design user tables flexibly according to the needs of the project, but also use Django's powerful authentication system.

From django.contrib.auth.models import AbstractUserclass UserInfo (AbstractUser): "" user Information Table "nid = models.AutoField (primary_key=True) phone = models.CharField (max_length=11, null=True, unique=True) def _ _ str__ (self): return self.username

Note: after extending the built-in auth_ user table as above, be sure to tell Django in settings.py that I am now using my newly defined UserInfo table for user authentication.

It is written as follows:

# refer to the User table that comes with Django. You need to set AUTH_USER_MODEL = "app name .UserInfo" when inheritance is used.

Note again: once we have specified the table to be used by the new authentication system, we need to recreate the table in the database instead of using the original default auth_user table.

At this point, I believe that everyone on the "Django to achieve built-in auth authentication system" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report