Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

您现在的位置:首页 > 技术文档 > Python网站web

Django框架用户注销功能实现方法分析

来源:中文源码网    浏览:162 次    日期:2024-05-17 06:15:14
【下载文档:  Django框架用户注销功能实现方法分析.txt 】


Django框架用户注销功能实现方法分析
本文实例讲述了Django框架用户注销功能实现方法。分享给大家供大家参考,具体如下:
HttpResponse()里有个delete_cookie()方法专门用来删除cookie
我们到此来完整的实现一下:访问首页如果没有登录,就跳转到登录页面,登录成功之后再跳转回来的过程。
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("")
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("")
return r
首页模板index.html




首页


这是首页,当前登录用户是:{{ username }}


{% ifequal username "guest" %}

登录


{% else %}

安装退出


{% endifequal %}


其中用到了Django的模板语法
希望本文所述对大家基于Django框架的Python程序设计有所帮助。

相关内容