Django框架用戶注銷(xiāo)功能實(shí)現(xiàn)方法分析
本文實(shí)例講述了Django框架用戶注銷(xiāo)功能實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
HttpResponse()里有個(gè)delete_cookie()方法專(zhuān)門(mén)用來(lái)刪除cookie
我們到此來(lái)完整的實(shí)現(xiàn)一下:訪問(wèn)首頁(yè)如果沒(méi)有登錄,就跳轉(zhuǎn)到登錄頁(yè)面,登錄成功之后再跳轉(zhuǎn)回來(lái)的過(guò)程。
3個(gè)方法,index、login、logout
# coding:utf-8
from django.shortcuts import render,render_to_response
# Create your views here.
from django.http import HttpResponse
from UserClass import UserLogin
def index(request):
msg = {'username':'guest'}
if request.COOKIES.get('userlogin_username') != None :
msg['username'] = request.COOKIES.get('userlogin_username')
myReponse = render_to_response("index.html",msg)
return myReponse
def login(request):
msg = {'result': ''}
if request.method == 'POST':
getUserName = request.POST.get('username')
getPwd = request.POST.get('pwd')
# 實(shí)例化UserLogin類(lèi)
loginObj = UserLogin(getUserName,getPwd)
if loginObj.isLogin():
myReponse = HttpResponse("<script>self.location='/index'</script>")
myReponse.set_cookie('userlogin_username',getUserName,3600)
return myReponse
else:
msg['result'] = '用戶名或密碼錯(cuò)誤'
myReponse = render_to_response("login.html", msg)
return myReponse
# 用戶注銷(xiāo)
def logout(request):
r = HttpResponse()
r.delete_cookie('userlogin_username')
r.write("<script>self.location='/index'</script>")
return r
首頁(yè)模板index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首頁(yè)</title>
</head>
<body>
<h2>這是首頁(yè),當(dāng)前登錄用戶是:{{ username }}</h2>
{% ifequal username "guest" %}
<p><a href="/login" rel="external nofollow" >登錄</a></p>
{% else %}
<p><a href="/logout" rel="external nofollow" >安裝退出</a></p>
{% endifequal %}
</body>
</html>
其中用到了Django的模板語(yǔ)法
希望本文所述對(duì)大家基于Django框架的Python程序設(shè)計(jì)有所幫助。
- django用戶注冊(cè)、登錄、注銷(xiāo)和用戶擴(kuò)展的示例
- Django實(shí)戰(zhàn)之用戶認(rèn)證(用戶登錄與注銷(xiāo))
- django用戶登錄和注銷(xiāo)的實(shí)現(xiàn)方法
- django實(shí)現(xiàn)用戶登陸功能詳解
- Django 生成登陸驗(yàn)證碼代碼分享
- Django 登陸驗(yàn)證碼和中間件的實(shí)現(xiàn)
- Django框架首頁(yè)和登錄頁(yè)分離操作示例
- Django利用cookie保存用戶登錄信息的簡(jiǎn)單實(shí)現(xiàn)方法
- Django框架登錄加上驗(yàn)證碼校驗(yàn)實(shí)現(xiàn)驗(yàn)證功能示例
- Django框架實(shí)現(xiàn)的普通登錄案例【使用POST方法】
- Django實(shí)現(xiàn)單用戶登錄的方法示例
- django與小程序?qū)崿F(xiàn)登錄驗(yàn)證功能的示例代碼
相關(guān)文章
python tkinter圖形界面代碼統(tǒng)計(jì)工具
這篇文章主要為大家詳細(xì)介紹了python tkinter圖形界面代碼統(tǒng)計(jì)工具,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
Python實(shí)現(xiàn)PDF頁(yè)面的刪除與添加功能
在處理PDF文檔的過(guò)程中,我們時(shí)常會(huì)需要對(duì)PDF文檔中的頁(yè)面進(jìn)行編輯操作的情況,如插入和刪除頁(yè)面,通過(guò)添加和刪除PDF頁(yè)面,我們可以增加內(nèi)容或?qū)Σ恍枰膬?nèi)容進(jìn)行刪除,本文將介紹如何使用Python代碼實(shí)現(xiàn)在PDF文檔中添加和刪除頁(yè)面2024-04-04
django xadmin中form_layout添加字段顯示方式
這篇文章主要介紹了django xadmin中form_layout添加字段顯示方式,具有很好的 參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
基于Python實(shí)現(xiàn)圖片瀏覽器的應(yīng)用程序
圖像瀏覽器應(yīng)用程序是一種非常常見(jiàn)和實(shí)用的工具,這篇文章就來(lái)為大家介紹一下如何使用Python編程語(yǔ)言和wxPython庫(kù)創(chuàng)建一個(gè)簡(jiǎn)單的圖像瀏覽器應(yīng)用程序,感興趣的可以了解下2023-10-10

