7.15. Django user Authentication (Auth) component

发布时间 :2025-10-25 12:24:14 UTC      

The Django user authentication (Auth) component is generally used in the login registration of users to determine whether the current user is legitimate or not, and to jump to the login success or failure page.

The Django user Authentication (Auth) component needs to be imported into the auth module:

# 认证模块
from django.contrib import auth

# 对应数据库
from django.contrib.auth.models import User

The return value is the user object.

There are three ways to create user objects:

  • create() Create an ordinary user with a password in clear text

  • create_user() Create an ordinary user with a password in ciphertext

  • create_superuser() Create a super user whose password is in ciphertext and pass an extra mailbox email parameter.

参数:

  • Username: user name.

  • Password: password.

  • Email: mailbox (add an extra email for the create_superuser method).

from django.contrib.auth.models import User
User.objects.create(username='runboo',password='123')

image0

from django.contrib.auth.models import User
User.objects.create_user(username='runbooo',password='123')

image1

from django.contrib.auth.models import User
User.objects.create_superuser(username='runboooo',password='123',email='runboo@163.com')

image2

Validate the user’s user name and password using the authenticate () method to filter out the user object from the required auth_user table.

Import before use:

from django.contrib import auth

Parameters:

  • Username: user name

  • Password: password

返回值: If the validation is successful, the user object is returned, otherwise, None is returned.

7.15.1. Example

def login(request):
    if request.method == "GET":
        return render(request, "login.html")
    username = request.POST.get("username")
    password = request.POST.get("pwd")
    valid_num = request.POST.get("valid_num")
    keep_str = request.session.get("keep_str")
    if keep_str.upper() == valid_num.upper():
        user_obj = auth.authenticate(username=username,
password=password)
        print(user_obj.username)

image3

Add session to the successful user and assign request.user to the user object.

Log in using the login () method. Import before use:

from django.contrib import auth

Parameters:

  • Request: user object

Return value: None

7.15.2. Example

def login(request):
    if request.method == "GET":
        return render(request, "login.html")
    username = request.POST.get("username")
    password = request.POST.get("pwd")
    valid_num = request.POST.get("valid_num")
    keep_str = request.session.get("keep_str")
    if keep_str.upper() == valid_num.upper():
        user_obj = auth.authenticate(username=username,
password=password)
        print(user_obj.username)
        if not user_obj:
            return redirect("/login/")
        else:
            auth.login(request, user_obj)
            path = request.GET.get("next") or "/index/"
            print(path)
            return redirect(path)
    else:
        return redirect("/login/")

image4

The logout () method is used to log out the user, which needs to clear the session information and assign the request.user to an anonymous user.

Import before use:

from django.contrib import auth

Parameters:

  • Request: user object

Return value: None

7.15.3. Example

def logout(request):
    ppp = auth.logout(request)
    print(ppp) # None
    return redirect("/login/")

Set up a decorator to add a decorator to the pages that need to be accessed after a successful login.

Import before use:

from django.contrib.auth.decorators import login_required

7.15.4. Example

from django.contrib.auth.decorators import login_required
@login_required
def index(request):
  return HttpResponse("index页面。。。")

Set which page to access from and which page to return after a successful login.

解析:

Django returns the login page to the user if the user is not logged in when the user visits the page.

At this point, the URL of the login page is followed by a parameter: the URL of the page visited by the next= user.

Therefore, set the value of the redirected URL to the next parameter after the user has successfully logged in.

However, if the user enters the login page logi,request.GET.get (“next”) at the beginning, there is no value, so add or after it, and you can set the custom returned page.

7.15.5. Example

# 如果直接输入 login、get() 就取不到值,path 可以自定义设置返回的页面
path = request.GET.get("next") or "/index/"
return redirect(path)

image5

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.