欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django認(rèn)證系統(tǒng)實(shí)現(xiàn)的web頁(yè)面實(shí)現(xiàn)代碼

 更新時(shí)間:2019年08月12日 10:32:43   作者:Leslie-x  
這篇文章主要介紹了Django認(rèn)證系統(tǒng)實(shí)現(xiàn)的web頁(yè)面實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

結(jié)合數(shù)據(jù)庫(kù)、ajax、js、Djangoform表單和認(rèn)證系統(tǒng)的web頁(yè)面

一:數(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" style="margin-top: 50px">
  <div class="panel panel-primary">

   <div class="panel-heading"><h4>用戶(hù)詳細(xì)信息</h4></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 style="margin-top: 20px">
   <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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Django中prefetch_related()函數(shù)優(yōu)化實(shí)戰(zhàn)指南

    Django中prefetch_related()函數(shù)優(yōu)化實(shí)戰(zhàn)指南

    我們可以利用Django框架中select_related和prefetch_related函數(shù)對(duì)數(shù)據(jù)庫(kù)查詢(xún)優(yōu)化,這篇文章主要給大家介紹了關(guān)于Django中prefetch_related()函數(shù)優(yōu)化的相關(guān)資料,需要的朋友可以參考下
    2022-11-11
  • python之驗(yàn)證碼生成(gvcode與captcha)

    python之驗(yàn)證碼生成(gvcode與captcha)

    這篇文章主要介紹了python之驗(yàn)證碼生成(gvcode與captcha),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • Python3 讀、寫(xiě)Excel文件的操作方法

    Python3 讀、寫(xiě)Excel文件的操作方法

    這篇文章主要介紹了Python3 讀、寫(xiě)Excel文件的操作方法,需要的朋友可以參考下
    2018-10-10
  • 詳解Python中的Numpy、SciPy、MatPlotLib安裝與配置

    詳解Python中的Numpy、SciPy、MatPlotLib安裝與配置

    這篇文章主要介紹了詳解Python中的Numpy、SciPy、MatPlotLib安裝與配置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Python netmiko模塊的使用

    Python netmiko模塊的使用

    這篇文章主要介紹了Python netmiko模塊的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • PyQt與pycharm的結(jié)合使用教程

    PyQt與pycharm的結(jié)合使用教程

    這篇文章主要介紹了PyQt的使用與pycharm的結(jié)合,主要包括環(huán)境安裝,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • python 刪除系統(tǒng)中的文件(按時(shí)間,大小,擴(kuò)展名)

    python 刪除系統(tǒng)中的文件(按時(shí)間,大小,擴(kuò)展名)

    這篇文章主要介紹了python 如何刪除系統(tǒng)中的文件,分別按時(shí)間,大小,擴(kuò)展名刪除,滿(mǎn)足不同需求,感興趣的朋友可以了解下
    2020-11-11
  • python實(shí)現(xiàn)控制臺(tái)打印的方法

    python實(shí)現(xiàn)控制臺(tái)打印的方法

    今天小編就為大家分享一篇python實(shí)現(xiàn)控制臺(tái)打印的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • python隊(duì)列通信:rabbitMQ的使用(實(shí)例講解)

    python隊(duì)列通信:rabbitMQ的使用(實(shí)例講解)

    下面小編就為大家分享一篇python隊(duì)列通信:rabbitMQ的使用(實(shí)例講解),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • 介紹Python中的__future__模塊

    介紹Python中的__future__模塊

    這篇文章主要介紹了介紹Python中的__future__模塊,__future__模塊使得在Python2.x的版本下能夠兼容更多的Python3.x的特性,需要的朋友可以參考下
    2015-04-04

最新評(píng)論