您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關使用Django認證系統怎么實現web頁面,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
一:數據模塊
擴展了Django中的user表,增加了自定義的字段
from django.db import models from django.contrib.auth.models import AbstractUser # Create your models here. class UserInfo(AbstractUser): phone = models.CharField(max_length=11) gender = models.CharField(max_length=2)
二:路由系統
from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^register/',views.register), url(r'^login/',views.login_view), url(r'^home/',views.home), url(r'^logout/',views.logout_view), url(r'^modify_pwd/',views.modify_pwd), url(r'^$',views.home), ]
三:視圖系統
from django.shortcuts import render, redirect from django.contrib.auth.decorators import login_required from django.http import JsonResponse from django.contrib.auth import authenticate, login,logout from app01 import forms from app01.models import UserInfo # Create your views here. def register(request): form_obj = forms.Reg_form() if request.method == 'POST': form_obj = forms.Reg_form(request.POST) if form_obj.is_valid(): info_dic = form_obj.cleaned_data sex_dic = {'1':'男','2':'女','3':'保密'} info_dic['gender']=sex_dic[info_dic['gender']] UserInfo.objects.create_user( username=info_dic['username'], password = info_dic['pwd'], gender=info_dic['gender'], phone =info_dic['phone'] ) return redirect('/login/') return render(request, "register.html",{'form_obj':form_obj}) def login_view(request): if request.method == 'POST': username = request.POST.get('username') pwd = request.POST.get('pwd') user = authenticate(username=username, password=pwd) if user: login(request, user) data = {'code':1} else: data = {'code': 0,'msg':'用戶名或密碼錯誤'} return JsonResponse(data) return render(request, 'login.html') @login_required def logout_view(request): logout(request) return redirect('/login/') @login_required def home(request): user_id = request.session['_auth_user_id'] use_obj = request.user return render(request,'home.html',{'user':use_obj}) @login_required def modify_pwd(request): if request.method == 'POST': old_pwd = request.POST.get('old_pwd') pwd = request.POST.get('pwd') re_pwd = request.POST.get('re_pwd') user_obj = request.user if user_obj.check_password(old_pwd): if re_pwd == pwd: user_obj.set_password(pwd) user_obj.save() data = {'code': 1} else: data = {'code': 0, 'msg': '兩次輸入密碼不一致'} else: data = {'code': 0, 'msg': '原始密碼輸入錯誤'} return JsonResponse(data) return render(request,'modify_pwd.html')
四:form表單
#!/usr/bin/env python # -*- coding: UTF-8 -*- # Author:YiJun from django import forms from app01 import models from django.forms import widgets from django.core.exceptions import ValidationError # 導入異常 import re # Create your views here. class Reg_form(forms.Form): # 用戶名表單 username = forms.CharField( min_length=4, label="設置用戶名", error_messages={ "required": "不能為空", "invalid": "格式錯誤", "min_length": "用戶名最少4個字符" }, widget=widgets.TextInput( attrs={ 'class': "form-control", 'placeholder': '用戶名' }) ) # 用戶密碼設置表單 pwd = forms.CharField( min_length=6, label="設置密碼", widget=forms.widgets.PasswordInput( attrs={ 'class': 'form-control', 'placeholder': '密碼'}, render_value=True, ), error_messages={ "required": "不能為空", "invalid": "格式錯誤", "min_length": "密碼至少6位" } ) # 用戶密碼確認表單 r_pwd = forms.CharField( min_length=6, label="確認密碼", widget=forms.widgets.PasswordInput( attrs={ 'class': 'form-control', 'placeholder': '確認密碼'}, render_value=True, ), error_messages={ "required": "不能為空", "invalid": "格式錯誤", "min_length": "密碼至少6位" } ) # 用戶性別選擇表單 gender = forms.ChoiceField( choices=((1, "男"), (2, "女"), (3, "保密")), label="性別", initial=3, widget=forms.widgets.RadioSelect ) # 用戶手機號碼表單 phone = forms.CharField( label="手機號碼", max_length=11, min_length=11, error_messages={ "required": "不能為空", "invalid": "格式錯誤", "min_length": "手機號碼至少11位", "max_length": "手機號碼最多11位", }, widget=widgets.TextInput(attrs={'class': "form-control",'placeholder': '手機號碼'}) ) def clean_phone(self): value = self.cleaned_data['phone'] expression = re.compile('^1[3589][0-9]{9}') if not expression.search(value).group(): raise ValidationError('請輸入正確的手機號碼') else: return value def clean_username(self): value = self.cleaned_data['username'] if models.UserInfo.objects.filter(username=value): raise ValidationError('用戶名已經注冊') else: return value def clean(self): pwd = self.cleaned_data.get("pwd") r_pwd = self.cleaned_data.get("r_pwd") if pwd != r_pwd: self.add_error("r_pwd", "兩次輸入的密碼不一致!") # 兩次輸入的密碼不一致 raise ValidationError("兩次輸入的密碼不一致!") else: self.cleaned_data.pop('r_pwd') return self.cleaned_data
五:模板系統
注冊頁面
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <link rel="stylesheet" href="/static/bootstrap-3.3.7/css/bootstrap.min.css" rel="external nofollow" > <title>Document</title> </head> <body> <div class="container"> <div class="row" > <div class="panel panel-primary"> <div class="panel-heading"><h5>用戶詳細信息</h5></div> <div class="panel-body"> </div> <!-- Table --> <div class="table-responsive"> <table class="table table-bordered"> <thead> <tr> <th>#</th> <th>用戶名</th> <th>手機號碼</th> <th>上次登陸時間</th> <th>注冊時間</th> <th>用戶性別</th> </tr> </thead> <tbody> <tr> <th scope="row">1</th> <td>{{ user.username }}</td> <td>{{ user.phone }}</td> <td>{{ user.last_login|date:'Y-m-d H:i:s' }}</td> <td>{{ user.date_joined|date:'Y-m-d H:i:s' }}</td> <td>{{ user.gender }}</td> </tr> </tbody> </table> </div> </div> <div > <a class="btn btn-info " href="/modify_pwd/" rel="external nofollow" >修改密碼</a> <a class="btn btn-danger pull-right" href="/logout/" rel="external nofollow" >注銷</a> </div> </div> </div> <script src="/static/jquery-3.3.1.min.js"></script> <script src="/static/bootstrap-3.3.7/js/bootstrap.min.js"></script> </body> </html> home.html
關于使用Django認證系統怎么實現web頁面就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。