国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

詳解Django框架中用戶的登錄和退出的實現

2020-01-04 17:59:32
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了詳解Django框架中用戶的登錄和退出的實現,Django是重多Python人氣框架中最為知名的一個,需要的朋友可以參考下
 


Django 提供內置的視圖(view)函數用于處理登錄和退出 (以及其他奇技淫巧),但在開始前,我們來看看如何手工登錄和退出。 Django提供兩個函數來執行django.contrib.auth/中的動作 : authenticate()和login()。

認證給出的用戶名和密碼,使用 authenticate() 函數。它接受兩個參數,用戶名 username 和 密碼 password ,并在密碼對給出的用戶名合法的情況下返回一個 User 對象。 如果密碼不合法,authenticate()返回None。
 

  1. >>> from django.contrib import auth 
  2. >>> user = auth.authenticate(username='john', password='secret'
  3. >>> if user is not None: 
  4. ...   print "Correct!" 
  5. ... else
  6. ...   print "Invalid password." 
?

authenticate() 只是驗證一個用戶的證書而已。 而要登錄一個用戶,使用 login() 。該函數接受一個 HttpRequest 對象和一個 User 對象作為參數并使用Django的會話( session )框架把用戶的ID保存在該會話中。

下面的例子演示了如何在一個視圖中同時使用 authenticate() 和 login() 函數:
 

  1. from django.contrib import auth 
  2.  
  3. def login_view(request): 
  4.   username = request.POST.get('username'''
  5.   password = request.POST.get('password'''
  6.   user = auth.authenticate(username=username, password=password) 
  7.   if user is not None and user.is_active: 
  8.     # Correct password, and the user is marked "active" 
  9.     auth.login(request, user) 
  10.     # Redirect to a success page. 
  11.     return HttpResponseRedirect("/account/loggedin/"
  12.   else
  13.     # Show an error page 
  14.     return HttpResponseRedirect("/account/invalid/"
?

注銷一個用戶,在你的視圖中使用 django.contrib.auth.logout() 。 它接受一個HttpRequest對象并且沒有返回值。
 

  1. from django.contrib import auth 
  2.  
  3. def logout_view(request): 
  4.   auth.logout(request) 
  5.   # Redirect to a success page. 
  6.   return HttpResponseRedirect("/account/loggedout/"
?

注意,即使用戶沒有登錄, logout() 也不會拋出任何異常。

在實際中,你一般不需要自己寫登錄/登出的函數;認證系統提供了一系例視圖用來處理登錄和登出。 使用認證視圖的第一步是把它們寫在你的URLconf中。 你需要這樣寫:
 

  1. from django.contrib.auth.views import login, logout 
  2.  
  3. urlpatterns = patterns(''
  4.   # existing patterns here... 
  5.   (r'^accounts/login/$', login), 
  6.   (r'^accounts/logout/$', logout), 
?

/accounts/login/ 和 /accounts/logout/ 是Django提供的視圖的默認URL。

缺省情況下, login 視圖渲染 registragiton/login.html 模板(可以通過視圖的額外參數 template_name 修改這個模板名稱)。 這個表單必須包含 username 和 password 域。如下示例: 一個簡單的 template 看起來是這樣的
 

  1. {% extends "base.html" %} 
  2.  
  3. {% block content %} 
  4.  
  5.  {% if form.errors %} 
  6.   <p class="error">Sorry, that's not a valid username or password</p> 
  7.  {% endif %} 
  8.  
  9.  <form action="" method="post"
  10.   <label for="username">User name:</label> 
  11.   <input type="text" name="username" value="" id="username"
  12.   <label for="password">Password:</label> 
  13.   <input type="password" name="password" value="" id="password"
  14.  
  15.   <input type="submit" value="login" /> 
  16.   <input type="hidden" name="next" value="{{ next|escape }}" /> 
  17.  </form> 
  18.  
  19. {% endblock %} 
?

如果用戶登錄成功,缺省會重定向到 /accounts/profile 。 你可以提供一個保存登錄后重定向URL的next隱藏域來重載它的行為。 也可以把值以GET參數的形式發送給視圖函數,它會以變量next的形式保存在上下文中,這樣你就可以把它用在隱藏域上了。

logout視圖有一些不同。 默認情況下它渲染 registration/logged_out.html 模板(這個視圖一般包含你已經成功退出的信息)。 視圖中還可以包含一個參數 next_page 用于退出后重定向。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 濮阳市| 信宜市| 五大连池市| 淮滨县| 洪雅县| 孝感市| 阿荣旗| 萝北县| 政和县| 成武县| 惠水县| 格尔木市| 龙南县| 永川市| 临夏县| 四子王旗| 丰台区| 临汾市| 玛纳斯县| 祥云县| 南华县| 凤凰县| 新绛县| 祥云县| 万全县| 冕宁县| 二连浩特市| 泗洪县| 容城县| 勃利县| 武宣县| 海原县| 怀柔区| 白水县| 桓仁| 彩票| 乐安县| 大关县| 南召县| 广宗县| 九江市|