您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)使用Django認(rèn)證系統(tǒng)怎么實(shí)現(xiàn)web頁(yè)面,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
一:數(shù)據(jù)模塊
擴(kuò)展了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)
二:路由系統(tǒng)
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), ]
三:視圖系統(tǒng)
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':'用戶(hù)名或密碼錯(cuò)誤'} 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': '原始密碼輸入錯(cuò)誤'} 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 # 導(dǎo)入異常 import re # Create your views here. class Reg_form(forms.Form): # 用戶(hù)名表單 username = forms.CharField( min_length=4, label="設(shè)置用戶(hù)名", error_messages={ "required": "不能為空", "invalid": "格式錯(cuò)誤", "min_length": "用戶(hù)名最少4個(gè)字符" }, widget=widgets.TextInput( attrs={ 'class': "form-control", 'placeholder': '用戶(hù)名' }) ) # 用戶(hù)密碼設(shè)置表單 pwd = forms.CharField( min_length=6, label="設(shè)置密碼", widget=forms.widgets.PasswordInput( attrs={ 'class': 'form-control', 'placeholder': '密碼'}, render_value=True, ), error_messages={ "required": "不能為空", "invalid": "格式錯(cuò)誤", "min_length": "密碼至少6位" } ) # 用戶(hù)密碼確認(rèn)表單 r_pwd = forms.CharField( min_length=6, label="確認(rèn)密碼", widget=forms.widgets.PasswordInput( attrs={ 'class': 'form-control', 'placeholder': '確認(rèn)密碼'}, render_value=True, ), error_messages={ "required": "不能為空", "invalid": "格式錯(cuò)誤", "min_length": "密碼至少6位" } ) # 用戶(hù)性別選擇表單 gender = forms.ChoiceField( choices=((1, "男"), (2, "女"), (3, "保密")), label="性別", initial=3, widget=forms.widgets.RadioSelect ) # 用戶(hù)手機(jī)號(hào)碼表單 phone = forms.CharField( label="手機(jī)號(hào)碼", max_length=11, min_length=11, error_messages={ "required": "不能為空", "invalid": "格式錯(cuò)誤", "min_length": "手機(jī)號(hào)碼至少11位", "max_length": "手機(jī)號(hào)碼最多11位", }, widget=widgets.TextInput(attrs={'class': "form-control",'placeholder': '手機(jī)號(hào)碼'}) ) 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('請(qǐng)輸入正確的手機(jī)號(hào)碼') else: return value def clean_username(self): value = self.cleaned_data['username'] if models.UserInfo.objects.filter(username=value): raise ValidationError('用戶(hù)名已經(jīng)注冊(cè)') 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
五:模板系統(tǒng)
注冊(cè)頁(yè)面
<!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>用戶(hù)詳細(xì)信息</h5></div> <div class="panel-body"> </div> <!-- Table --> <div class="table-responsive"> <table class="table table-bordered"> <thead> <tr> <th>#</th> <th>用戶(hù)名</th> <th>手機(jī)號(hào)碼</th> <th>上次登陸時(shí)間</th> <th>注冊(cè)時(shí)間</th> <th>用戶(hù)性別</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" >注銷(xiāo)</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
關(guān)于使用Django認(rèn)證系統(tǒng)怎么實(shí)現(xiàn)web頁(yè)面就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。