本文包含文件(Django其他的文件内容这里不写,用到的就下面6个):
auth_index.html
auth_login.html
auth_reg.html
auth_set_password.html
urls.py
views.py
auth_index.html
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Apollo</title> <!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- 最新的 Bootstrap 核心 JavaScript 文件 --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> </head> <body> 当前用户:{{ name }} 当前用户:{{ request.user.username }} <a href="{% url 'auth_logout' %}">注销</a> <a href="{% url 'auth_set_password' %}">修改密码</a> </body> </html>
auth_login.html
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Apollo</title> <!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- 最新的 Bootstrap 核心 JavaScript 文件 --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> </head> <body> <form action="" method="post"> {% csrf_token %} <label for="usr"></label> <input type="text" name="usr" id="usr"> <label for="pwd"></label> <input type="password" name="pwd" id="pwd"> <input type="submit" value="登录" class="btn btn-success"> <p>{{ error }}</p> </form> </body> </html>
auth_reg.html
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Apollo</title> <!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- 最新的 Bootstrap 核心 JavaScript 文件 --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> </head> <body> 注册页面 <form action="" method="post"> {% csrf_token %} <label for="usr"></label> <input type="text" name="usr" id="usr"> <label for="pwd"></label> <input type="password" name="pwd" id="pwd"> <input type="submit" value="注册" class="btn btn-success"> </form> </body> </html>
auth_set_password.html
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Apollo-修改密码</title> <!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- 最新的 Bootstrap 核心 JavaScript 文件 --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> </head> <body> 修改密码 <form action="" method="post"> {% csrf_token %} <label for="usr"></label> <input type="text" name="usr" id="usr" value="{{ usr }}"> <label for="pwd"></label> <input type="password" name="pwd" id="pwd"> <input type="submit" value="修改密码" class="btn btn-default btn-xs"> </form> </body> </html>
urls.py
from django.views.generic.base import RedirectView
from django.contrib import admin
from django.urls import path
from coo import views
urlpatterns = [
path('admin/', admin.site.urls),
path("favicon.ico", RedirectView.as_view(url='static/favicon.ico')),
# Django组件Auth
path('auth_login/', views.auth_login, name='auth_login'),
path('auth_index/', views.auth_index, name='auth_index'),
path('auth_logout/', views.auth_logout, name='auth_logout'),
path('auth_reg/', views.auth_reg, name='auth_reg'),
path('auth_set_password/', views.auth_set_password, name='auth_set_password'),
]
views.py
from django.shortcuts import render, redirect, HttpResponse, reverse from django.contrib.auth.models import User from django.contrib import auth from coo.models import UserInfo # Create your views here. # ############### auth组件的login页面 ############### def auth_login(request): if request.method == 'GET': return render(request, 'auth_login.html') else: # 获取用户名和密码 usr = request.POST.get('usr') pwd = request.POST.get('pwd') # 数据库查询该用户是否存在 # authenticate去auth_user查询记录,查询成功返回用户对象,查询失败返回None user_obj = auth.authenticate(username=usr, password=pwd) # 验证通过,保存信息,跳转到首页 if user_obj: # 保存用户状态信息 auth.login(request, user_obj) # 重定向/auth_index/ return redirect("/auth_index/") # 验证不通过,返回登录页,提示用户错误原因 else: # 重定向/auth_login/ error = 'USERNAME AND PASSWORD DO NOT MATCH' return redirect("/auth_login/", {'error': error}) # ############### auth组件的index页面 ############### def auth_index(request): print(request.user.is_authenticated) # 用户不存在,重定向到登录页 if not request.user.is_authenticated: return redirect("/auth_login/") # 获取用户名 name = request.user.username # 验证通过,渲染首页auth_index return render(request, "auth_index.html", locals()) # ################# auth组件的reg页面 ################# def auth_reg(request): # 处理GET请求 if request.method == "GET": return render(request, "auth_reg.html") # 处理POST请求 else: # 获取注册用户名 user = request.POST.get("usr") # 获取注册用户密码 pwd = request.POST.get("pwd") # 创建用户 User.objects.create_user(username=user, password=pwd) # 注册成功,重定向到登录页面 return redirect("/auth_login/") # ################# auth组件的logout ################# def auth_logout(request): # 注销用户 auth.logout(request) # 注销后跳转到登录页面 return redirect("/auth_login/") # ################# auth组件的set_password ################# def auth_set_password(request): # 获取当前用户名 usr = User.objects.get(username=request.user.username) # 处理GET请求 if request.method == 'GET': # 将用户名返回到修改密码页面 return render(request, 'auth_set_password.html',{'usr':usr}) # 处理POST请求 else: # 更新密码,保存到数据库 pwd = request.POST.get('pwd') usr.set_password(raw_password=pwd) usr.save() # 更新完,跳转到登录页面 return redirect("/auth_login/")