Django框架用戶注銷功能實現(xiàn)方法分析
更新時間:2019年05月28日 09:27:52 作者:學習筆記666
這篇文章主要介紹了Django框架用戶注銷功能實現(xiàn)方法,結(jié)合實例形式分析了基于Django框架的刪除cookie實現(xiàn)用戶注銷功能的相關(guā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)登錄驗證功能的示例代碼
相關(guān)文章
python tkinter圖形界面代碼統(tǒng)計工具
這篇文章主要為大家詳細介紹了python tkinter圖形界面代碼統(tǒng)計工具,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09
django xadmin中form_layout添加字段顯示方式
這篇文章主要介紹了django xadmin中form_layout添加字段顯示方式,具有很好的 參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03

