一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語(yǔ)言編程技術(shù)及教程分享平臺(tái)!
分類(lèi)導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - Django實(shí)戰(zhàn)之用戶(hù)認(rèn)證(用戶(hù)登錄與注銷(xiāo))

Django實(shí)戰(zhàn)之用戶(hù)認(rèn)證(用戶(hù)登錄與注銷(xiāo))

2021-03-17 00:32Zhu_Julian Python

這篇文章主要介紹了Django實(shí)戰(zhàn)之用戶(hù)認(rèn)證(用戶(hù)登錄與注銷(xiāo)),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

上一篇中,我們已經(jīng)打開(kāi)了django自帶的用戶(hù)認(rèn)證模塊,并配置了數(shù)據(jù)庫(kù)連接,創(chuàng)建了相應(yīng)的表,本篇我們將在django自帶的用戶(hù)認(rèn)證的基礎(chǔ)上,實(shí)現(xiàn)自己個(gè)性化的用戶(hù)登錄和注銷(xiāo)模塊。

首先,我們自己定義一個(gè)用戶(hù)登錄表單(forms.py):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from django import forms
from django.contrib.auth.models import user
from bootstrap_toolkit.widgets import bootstrapdateinput, bootstraptextinput, bootstrapuneditableinput
 
class loginform(forms.form):
  username = forms.charfield(
    required=true,
    label=u"用戶(hù)名",
    error_messages={'required': '請(qǐng)輸入用戶(hù)名'},
    widget=forms.textinput(
      attrs={
        'placeholder':u"用戶(hù)名",
      }
    ),
  
  password = forms.charfield(
    required=true,
    label=u"密碼",
    error_messages={'required': u'請(qǐng)輸入密碼'},
    widget=forms.passwordinput(
      attrs={
        'placeholder':u"密碼",
      }
    ),
  
  def clean(self):
    if not self.is_valid():
      raise forms.validationerror(u"用戶(hù)名和密碼為必填項(xiàng)")
    else:
      cleaned_data = super(loginform, self).clean()

我們定義的用戶(hù)登錄表單有兩個(gè)域username和password,這兩個(gè)域都為必填項(xiàng)。

接下來(lái),我們定義用戶(hù)登錄視圖(views.py),在該視圖里實(shí)例化之前定義的用戶(hù)登錄表單

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from django.shortcuts import render_to_response,render,get_object_or_404
from django.http import httpresponse, httpresponseredirect
from django.contrib.auth.models import user
from django.contrib import auth
from django.contrib import messages
from django.template.context import requestcontext
 
from django.forms.formsets import formset_factory
from django.core.paginator import paginator, pagenotaninteger, emptypage
 
from bootstrap_toolkit.widgets import bootstrapuneditableinput
from django.contrib.auth.decorators import login_required
 
from .forms import loginform
 
def login(request):
  if request.method == 'get':
    form = loginform()
    return render_to_response('login.html', requestcontext(request, {'form': form,}))
  else:
    form = loginform(request.post)
    if form.is_valid():
      username = request.post.get('username', '')
      password = request.post.get('password', '')
      user = auth.authenticate(username=username, password=password)
      if user is not none and user.is_active:
        auth.login(request, user)
        return render_to_response('index.html', requestcontext(request))
      else:
        return render_to_response('login.html', requestcontext(request, {'form': form,'password_is_wrong':true}))
    else:
      return render_to_response('login.html', requestcontext(request, {'form': form,}))

該視圖實(shí)例化了之前定義的loginform,它的主要業(yè)務(wù)邏輯是:

1. 判斷必填項(xiàng)用戶(hù)名和密碼是否為空,如果為空,提示"用戶(hù)名和密碼為必填項(xiàng)”的錯(cuò)誤信息

2. 判斷用戶(hù)名和密碼是否正確,如果錯(cuò)誤,提示“用戶(hù)名或密碼錯(cuò)誤"的錯(cuò)誤信息

3. 登陸成功后,進(jìn)入主頁(yè)(index.html)

其中,登錄頁(yè)面的模板(login.html)定義如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<!doctype html>
{% load bootstrap_toolkit %}
{% load url from future %}
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>數(shù)據(jù)庫(kù)腳本發(fā)布系統(tǒng)</title>
  <meta name="description" content="">
  <meta name="author" content="朱顯杰">
  {% bootstrap_stylesheet_tag %}
  {% bootstrap_stylesheet_tag "responsive" %}
  <style type="text/css">
    body {
      padding-top: 60px;
    }
  </style>
  <!--[if lt ie 9]>
  <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
  {% bootstrap_javascript_tag %}
  {% block extra_head %}{% endblock %}
</head>
 
<body>
 
  {% if password_is_wrong %}
    <div class="alert alert-error">
      <button type="button" class="close" data-dismiss="alert">×</button>
      <h4>錯(cuò)誤!</h4>用戶(hù)名或密碼錯(cuò)誤
    </div>
  {% endif %
  <div class="well">
    <h1>數(shù)據(jù)庫(kù)腳本發(fā)布系統(tǒng)</h1>
    <p> </p>
    <form class="form-horizontal" action="" method="post">
      {% csrf_token %}
      {{ form|as_bootstrap:"horizontal" }}
      <p class="form-actions">
        <input type="submit" value="登錄" class="btn btn-primary">
        <a href="/contactme/" rel="external nofollow" rel="external nofollow" ><input type="button" value="忘記密碼" class="btn btn-danger"></a>
        <a href="/contactme/" rel="external nofollow" rel="external nofollow" ><input type="button" value="新員工?" class="btn btn-success"></a>
      </p>
    </form>
  </div>
 
</body>
</html>

最后還需要在urls.py里添加:

?
1
(r'^accounts/login/$', 'dbrelease_app.views.login'),

最終的效果如下:

1)當(dāng)在瀏覽器里輸入http://192.168.1.16:8000/accounts/login/,出現(xiàn)如下登陸界面:

Django實(shí)戰(zhàn)之用戶(hù)認(rèn)證(用戶(hù)登錄與注銷(xiāo))

2)當(dāng)用戶(hù)名或密碼為空時(shí),提示”用戶(hù)名和密碼為必填項(xiàng)",如下所示:

Django實(shí)戰(zhàn)之用戶(hù)認(rèn)證(用戶(hù)登錄與注銷(xiāo))

3)當(dāng)用戶(hù)名或密碼錯(cuò)誤時(shí),提示“用戶(hù)名或密碼錯(cuò)誤",如下所示:

Django實(shí)戰(zhàn)之用戶(hù)認(rèn)證(用戶(hù)登錄與注銷(xiāo))

4)如果用戶(hù)名和密碼都正確,進(jìn)入主頁(yè)(index.html)。

既然有l(wèi)ogin,當(dāng)然要有l(wèi)ogout,logout比較簡(jiǎn)單,直接調(diào)用django自帶用戶(hù)認(rèn)證系統(tǒng)的logout,然后返回登錄界面,具體如下(views.py):

?
1
2
3
4
@login_required
def logout(request):
  auth.logout(request)
  return httpresponseredirect("/accounts/login/")

上面@login_required表示只有用戶(hù)在登錄的情況下才能調(diào)用該視圖,否則將自動(dòng)重定向至登錄頁(yè)面。

urls.py里添加:

?
1
(r'^accounts/logout/$', 'dbrelease_app.views.logout'),

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

原文鏈接:https://blog.csdn.net/dbanote/article/details/11465447

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩欧美综合在线二区三区 | 国产一区二区免费视频 | 99er在线视频| 香蕉久久久久 | 热99精品只有里视频最新 | a4yy欧美一区二区三区 | 男女激情视频1000辣妞范 | v视影院| 欧美日韩精品一区二区三区视频播放 | 久久精品成人免费网站 | 国产欧美精品专区一区二区 | 欧美一区二区三区四区视频 | 午夜福利理论片在线播放 | 亚洲国产成人在人网站天堂 | 欧美贵妇vs高跟办公室 | 日本大尺度激情做爰叫床 | 好大好猛好爽好深视频免费 | 538亚洲欧美国产日韩在线精品 | 免费港剧在线观看港剧 | 娇妻被健身教练挺进小说阅读 | 91sao国产在线观看 | 亚洲尿尿 | 高h文恩好大好爽 | 国产高清好大好夹受不了了 | 香蕉国产成版人视频在线观看 | 久久毛片网站 | 欧美精品亚洲精品日韩1818 | naruto tube18动漫| 高清一区二区 | 欧美另类z0zxi| tobu8中国在线播放免费 | 高h肉爽文农民工 | 亚洲激情视频在线 | 顶级欧美做受xxx000 | 精品久久成人免费第三区 | 狠狠的撞进去嗯啊h女强男视频 | 亚洲欧美自偷自拍另类小说 | 日产乱码卡1卡2卡三卡四在线 | 成人一级黄色大片 | 91精品国产免费久久 | 香港三级系列在线播放 |