詳解Django框架中用戶的登錄和退出的實現(xiàn)
Django 提供內(nèi)置的視圖(view)函數(shù)用于處理登錄和退出 (以及其他奇技淫巧),但在開始前,我們來看看如何手工登錄和退出。 Django提供兩個函數(shù)來執(zhí)行django.contrib.auth\中的動作 : authenticate()和login()。
認(rèn)證給出的用戶名和密碼,使用 authenticate() 函數(shù)。它接受兩個參數(shù),用戶名 username 和 密碼 password ,并在密碼對給出的用戶名合法的情況下返回一個 User 對象。 如果密碼不合法,authenticate()返回None。
>>> from django.contrib import auth >>> user = auth.authenticate(username='john', password='secret') >>> if user is not None: ... print "Correct!" ... else: ... print "Invalid password."
authenticate() 只是驗證一個用戶的證書而已。 而要登錄一個用戶,使用 login() 。該函數(shù)接受一個 HttpRequest 對象和一個 User 對象作為參數(shù)并使用Django的會話( session )框架把用戶的ID保存在該會話中。
下面的例子演示了如何在一個視圖中同時使用 authenticate() 和 login() 函數(shù):
from django.contrib import auth
def login_view(request):
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:
# Correct password, and the user is marked "active"
auth.login(request, user)
# Redirect to a success page.
return HttpResponseRedirect("/account/loggedin/")
else:
# Show an error page
return HttpResponseRedirect("/account/invalid/")
注銷一個用戶,在你的視圖中使用 django.contrib.auth.logout() 。 它接受一個HttpRequest對象并且沒有返回值。
from django.contrib import auth
def logout_view(request):
auth.logout(request)
# Redirect to a success page.
return HttpResponseRedirect("/account/loggedout/")
注意,即使用戶沒有登錄, logout() 也不會拋出任何異常。
在實際中,你一般不需要自己寫登錄/登出的函數(shù);認(rèn)證系統(tǒng)提供了一系例視圖用來處理登錄和登出。 使用認(rèn)證視圖的第一步是把它們寫在你的URLconf中。 你需要這樣寫:
from django.contrib.auth.views import login, logout
urlpatterns = patterns('',
# existing patterns here...
(r'^accounts/login/$', login),
(r'^accounts/logout/$', logout),
)
/accounts/login/ 和 /accounts/logout/ 是Django提供的視圖的默認(rèn)URL。
缺省情況下, login 視圖渲染 registragiton/login.html 模板(可以通過視圖的額外參數(shù) template_name 修改這個模板名稱)。 這個表單必須包含 username 和 password 域。如下示例: 一個簡單的 template 看起來是這樣的
{% extends "base.html" %}
{% block content %}
{% if form.errors %}
<p class="error">Sorry, that's not a valid username or password</p>
{% endif %}
<form action="" method="post">
<label for="username">User name:</label>
<input type="text" name="username" value="" id="username">
<label for="password">Password:</label>
<input type="password" name="password" value="" id="password">
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next|escape }}" />
</form>
{% endblock %}
如果用戶登錄成功,缺省會重定向到 /accounts/profile 。 你可以提供一個保存登錄后重定向URL的next隱藏域來重載它的行為。 也可以把值以GET參數(shù)的形式發(fā)送給視圖函數(shù),它會以變量next的形式保存在上下文中,這樣你就可以把它用在隱藏域上了。
logout視圖有一些不同。 默認(rèn)情況下它渲染 registration/logged_out.html 模板(這個視圖一般包含你已經(jīng)成功退出的信息)。 視圖中還可以包含一個參數(shù) next_page 用于退出后重定向。
- 對Django 中request.get和request.post的區(qū)別詳解
- 基于Django URL傳參 FORM表單傳數(shù)據(jù) get post的用法實例
- 教你如何將 Sublime 3 打造成 Python/Django IDE開發(fā)利器
- Python+Django在windows下的開發(fā)環(huán)境配置圖解
- python Django連接MySQL數(shù)據(jù)庫做增刪改查
- Django如何自定義model創(chuàng)建數(shù)據(jù)庫索引的順序
- Django中對數(shù)據(jù)查詢結(jié)果進行排序的方法
- Django中幾種重定向方法
- Python的Django框架中forms表單類的使用方法詳解
- python Django模板的使用方法(圖文)
- Django objects.all()、objects.get()與objects.filter()之間的區(qū)別介紹
- 教你安裝python Django(圖文)
- Django中更新多個對象數(shù)據(jù)與刪除對象的方法
- Django框架中render_to_response()函數(shù)的使用方法
- Django靜態(tài)資源URL STATIC_ROOT的配置方法
- 解決Django migrate No changes detected 不能創(chuàng)建表的問題
- django啟動uwsgi報錯的解決方法
- Django讀取Mysql數(shù)據(jù)并顯示在前端的實例
- Django小白教程之Django用戶注冊與登錄
- Python3+Django get/post請求實現(xiàn)教程詳解
相關(guān)文章
Python?GUI實現(xiàn)PDF轉(zhuǎn)Word功能
這篇文章主要介紹了如何使用?wxPython?創(chuàng)建一個簡單的圖形用戶界面(GUI)應(yīng)用程序,結(jié)合?pdf2docx?庫,實現(xiàn)將?PDF?轉(zhuǎn)換為?Word?文檔的功能,需要的可以參考下2024-12-12
Python使用設(shè)計模式中的責(zé)任鏈模式與迭代器模式的示例
這篇文章主要介紹了Python使用設(shè)計模式中的責(zé)任鏈模式與迭代器模式的示例,責(zé)任鏈模式與迭代器模式都可以被看作為行為型的設(shè)計模式,需要的朋友可以參考下2016-03-03
Python lxml解析HTML并用xpath獲取元素的方法
今天小編就為大家分享一篇Python lxml解析HTML并用xpath獲取元素的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
詳解python ThreadPoolExecutor異常捕獲
本文主要介紹了詳解python ThreadPoolExecutor異常捕獲,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
python基礎(chǔ)之包的導(dǎo)入和__init__.py的介紹
這篇文章主要介紹了python基礎(chǔ)之包的導(dǎo)入和__init__.py的相關(guān)資料,需要的朋友可以參考下2018-01-01

