欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django框架用戶注銷功能實(shí)現(xiàn)方法分析

 更新時(shí)間:2019年05月28日 09:27:52   作者:學(xué)習(xí)筆記666  
這篇文章主要介紹了Django框架用戶注銷功能實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了基于Django框架的刪除cookie實(shí)現(xiàn)用戶注銷功能的相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Django框架用戶注銷功能實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:

HttpResponse()里有個(gè)delete_cookie()方法專門用來刪除cookie

我們到此來完整的實(shí)現(xiàn)一下:訪問首頁如果沒有登錄,就跳轉(zhuǎn)到登錄頁面,登錄成功之后再跳轉(zhuǎn)回來的過程。

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類
    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
# 用戶注銷
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>這是首頁,當(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的模板語法

希望本文所述對(duì)大家基于Django框架的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論