Django框架用戶注銷功能實現(xiàn)方法分析
本文實例講述了Django框架用戶注銷功能實現(xiàn)方法。分享給大家供大家參考,具體如下:
HttpResponse()
里有個delete_cookie()
方法專門用來刪除cookie
我們到此來完整的實現(xiàn)一下:訪問首頁如果沒有登錄,就跳轉(zhuǎn)到登錄頁面,登錄成功之后再跳轉(zhuǎn)回來的過程。
3個方法,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') # 實例化UserLogin類 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'] = '用戶名或密碼錯誤' myReponse = render_to_response("login.html", msg) return myReponse # 用戶注銷 def logout(request): r = HttpResponse() r.delete_cookie('userlogin_username') r.write("<script>self.location='/index'</script>") return r
首頁模板index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>首頁</title> </head> <body> <h2>這是首頁,當前登錄用戶是:{{ 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的模板語法
希望本文所述對大家基于Django框架的Python程序設計有所幫助。
- django用戶注冊、登錄、注銷和用戶擴展的示例
- Django實戰(zhàn)之用戶認證(用戶登錄與注銷)
- django用戶登錄和注銷的實現(xiàn)方法
- django實現(xiàn)用戶登陸功能詳解
- Django 生成登陸驗證碼代碼分享
- Django 登陸驗證碼和中間件的實現(xiàn)
- Django框架首頁和登錄頁分離操作示例
- Django利用cookie保存用戶登錄信息的簡單實現(xiàn)方法
- Django框架登錄加上驗證碼校驗實現(xiàn)驗證功能示例
- Django框架實現(xiàn)的普通登錄案例【使用POST方法】
- Django實現(xiàn)單用戶登錄的方法示例
- django與小程序?qū)崿F(xiàn)登錄驗證功能的示例代碼
相關文章
對python 矩陣轉(zhuǎn)置transpose的實例講解
下面小編就為大家分享一篇對python 矩陣轉(zhuǎn)置transpose的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04Python用61行代碼實現(xiàn)圖片像素化的示例代碼
這篇文章主要介紹了Python用61行代碼實現(xiàn)圖片像素化的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-12-12查看keras各種網(wǎng)絡結(jié)構(gòu)各層的名字方式
這篇文章主要介紹了查看keras各種網(wǎng)絡結(jié)構(gòu)各層的名字方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06pandas 數(shù)據(jù)歸一化以及行刪除例程的方法
今天小編就為大家分享一篇pandas 數(shù)據(jù)歸一化以及行刪除例程的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11