Session:
    session是服务器端生成保存的一个键值对 , session内部机制依赖于cookie 。 用户登录后返回给客户端一个随机字符串,客户端带着随机字符串访问服务器,用于验证是否登录。

用户登录并创建session:

def login(request):
"""
:type request: object
"""
message = ""
if request.method == "POST": #获取登录用户和密码 并验证
user = request.POST.get('username')
passwd = request.POST.get('password')
print(user, passwd)
c = models.Administrator.objects.filter(username=user, password=passwd).count()
if c:
#验证通过, 创建session键值对
request.session['is_login'] = True
request.session['username'] = user req = redirect('/index')
print(req)
# req.set_cookie('username',user)
return req
else:
#验证失败报错
message = "用户名或密码错误!"
return render(request, 'login.html', {'msg': message})

装饰器:用于判断是否登录

def auth(func):
def inner(request,*args,**kwargs):
is_login=request.session.get('is_login')
if is_login:
return func(request,*args,**kwargs)
else:
return redirect('/login')
return inner

注销登录

@auth
def logout(request): #session的键 session_key: dict_keys(['is_login', 'username'])
session_key=request.session.keys()
print("session_key: ", session_key) #值 session_values: dict_values([True, 'root'])
session_values=request.session.values()
print("session_values: ",session_values) #全部 session_items: dict_items([('is_login', True), ('username', 'root')])
session_items=request.session.items()
print("session_items: ",session_items) # session 的key (后台生成的随机字符串) key: rk2o5cr47ychj0kgk6a7c5854d1zosrv
key=request.session.session_key #
print("key: ",key) # 将所有Session失效日期小于当前日期的数据删除
request.session.clear_expired() # 检查 用户session的随机字符串 在数据库中是否
request.session.exists("session_key") #注销登录信息,但是数据库依然保留了key和values
request.session.clear()
session_items=request.session.items()
print("session_items: ",session_items) # 删除当前用户的所有Session数据
request.session.delete("session_key") #注销,删除session数据库表中的数据,达到注销的效果
request.session.delete(key)
return redirect("/login")

用面向对象的方法实现 登录和注销

from django import views
from django.utils.decorators import method_decorator def outer(func):
def inner(request, *args, **kwargs):
print(request.method)
return func(request, *args, **kwargs)
return inner class Order(views.View):
pass
# CBV
# @method_decorator(outer, name='dispatch')
class Login(views.View): # @method_decorator(outer)
def dispatch(self, request, *args, **kwargs):
ret = super(Login, self).dispatch(request, *args, **kwargs)
return ret def get(self,request, *args, **kwargs):
print('GET')
return render(request, 'login.html', {'msg': ''}) def post(self, request, *args, **kwargs):
print('POST')
user = request.POST.get('user')
pwd = request.POST.get('pwd')
c = models.Administrator.objects.filter(username=user, password=pwd).count()
if c:
request.session['is_login'] = True
request.session['username'] = user
rep = redirect('/index.html')
return rep
else:
message = "用户名或密码错误"
return render(request, 'login.html', {'msg': message})

html前端界面

<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<title>后台管理登录</title> <style>
/*
*
* Template Name: Fullscreen Login
* Description: Login Template with Fullscreen Background Slideshow
* Author: Anli Zaimi
* Author URI: http://azmind.com
*
*/ body {
background: #f8f8f8;
font-family: 'PT Sans', Helvetica, Arial, sans-serif;
text-align: center;
color: #fff;
background-color: aquamarine;
} .page-container {
margin: 120px auto 0 auto;
} h1 {
font-size: 30px;
font-weight: 700;
text-shadow: 0 1px 4px rgba(0,0,0,.2);
} form {
position: relative;
width: 305px;
margin: 15px auto 0 auto;
text-align: center;
} input {
width: 270px;
height: 42px;
margin-top: 25px;
padding: 0 15px;
background: #2d2d2d; /* browsers that don't support rgba */
background: rgba(45,45,45,.15);
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
border: 1px solid #3d3d3d; /* browsers that don't support rgba */
border: 1px solid rgba(255,255,255,.15);
-moz-box-shadow: 0 2px 3px 0 rgba(0,0,0,.1) inset;
-webkit-box-shadow: 0 2px 3px 0 rgba(0,0,0,.1) inset;
box-shadow: 0 2px 3px 0 rgba(0,0,0,.1) inset;
font-family: 'PT Sans', Helvetica, Arial, sans-serif;
font-size: 14px;
color: #fff;
text-shadow: 0 1px 2px rgba(0,0,0,.1);
-o-transition: all .2s;
-moz-transition: all .2s;
-webkit-transition: all .2s;
-ms-transition: all .2s;
} input:-moz-placeholder { color: #fff; }
input:-ms-input-placeholder { color: #fff; }
input::-webkit-input-placeholder { color: #fff; } input:focus {
outline: none;
-moz-box-shadow:
0 2px 3px 0 rgba(0,0,0,.1) inset,
0 2px 7px 0 rgba(0,0,0,.2);
-webkit-box-shadow:
0 2px 3px 0 rgba(0,0,0,.1) inset,
0 2px 7px 0 rgba(0,0,0,.2);
box-shadow:
0 2px 3px 0 rgba(0,0,0,.1) inset,
0 2px 7px 0 rgba(0,0,0,.2);
} button {
cursor: pointer;
width: 300px;
height: 44px;
margin-top: 25px;
padding: 0;
background: #ef4300;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
border-radius: 6px;
border: 1px solid #ff730e;
-moz-box-shadow:
0 15px 30px 0 rgba(255,255,255,.25) inset,
0 2px 7px 0 rgba(0,0,0,.2);
-webkit-box-shadow:
0 15px 30px 0 rgba(255,255,255,.25) inset,
0 2px 7px 0 rgba(0,0,0,.2);
box-shadow:
0 15px 30px 0 rgba(255,255,255,.25) inset,
0 2px 7px 0 rgba(0,0,0,.2);
font-family: 'PT Sans', Helvetica, Arial, sans-serif;
font-size: 14px;
font-weight: 700;
color: #fff;
text-shadow: 0 1px 2px rgba(0,0,0,.1);
-o-transition: all .2s;
-moz-transition: all .2s;
-webkit-transition: all .2s;
-ms-transition: all .2s;
} button:hover {
-moz-box-shadow:
0 15px 30px 0 rgba(255,255,255,.15) inset,
0 2px 7px 0 rgba(0,0,0,.2);
-webkit-box-shadow:
0 15px 30px 0 rgba(255,255,255,.15) inset,
0 2px 7px 0 rgba(0,0,0,.2);
box-shadow:
0 15px 30px 0 rgba(255,255,255,.15) inset,
0 2px 7px 0 rgba(0,0,0,.2);
} button:active {
-moz-box-shadow:
0 15px 30px 0 rgba(255,255,255,.15) inset,
0 2px 7px 0 rgba(0,0,0,.2);
-webkit-box-shadow:
0 15px 30px 0 rgba(255,255,255,.15) inset,
0 2px 7px 0 rgba(0,0,0,.2);
box-shadow:
0 5px 8px 0 rgba(0,0,0,.1) inset,
0 1px 4px 0 rgba(0,0,0,.1); border: 0px solid #ef4300;
} .error {
display: none;
position: absolute;
top: 27px;
right: -55px;
width: 40px;
height: 40px;
background: #2d2d2d; /* browsers that don't support rgba */
background: rgba(45,45,45,.25);
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
border-radius: 8px;
} .error span {
display: inline-block;
margin-left: 2px;
font-size: 40px;
font-weight: 700;
line-height: 40px;
text-shadow: 0 1px 2px rgba(0,0,0,.1);
-o-transform: rotate(45deg);
-moz-transform: rotate(45deg);
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg); } </style>
</head> <body> <div class="page-container">
<h1>登录</h1>
<form action="" method="post">
{% csrf_token %}
<input type="text" name="username" class="username" placeholder="用户名">
<input type="password" name="password" class="password" placeholder="密码">
<span style="color: red">{{ msg }}</span>
<button type="submit">提交</button>
<div class="error"><span>+</span></div>
</form>
</div>
</body> <script src="/static/jquery-2.1.4.min.js"></script>
<script>
jQuery(document).ready(function() {
$('.page-container form').submit(function(){
var username = $(this).find('.username').val();
var password = $(this).find('.password').val();
if(username == '') {
$(this).find('.error').fadeOut('fast', function(){
$(this).css('top', '27px');
});
$(this).find('.error').fadeIn('fast', function(){
$(this).parent().find('.username').focus();
});
return false;
}
if(password == '') {
$(this).find('.error').fadeOut('fast', function(){
$(this).css('top', '96px');
});
$(this).find('.error').fadeIn('fast', function(){
$(this).parent().find('.password').focus();
});
return false;
}
}); $('.page-container form .username, .page-container form .password').keyup(function(){
$(this).parent().find('.error').fadeOut('fast');
}); });
</script> </html>

登录界面

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %} welcome {% endblock %}</title>
<style> *{
margin: 0px;
padding: 0px;
}
.page_head{
position: absolute;
width: 100%;
top: 0;
left: 0;
height: 48px;
background-color: aquamarine;
}
.page_head_memu{
position: relative;
float: left;
height: 48px;
width: 100%;
line-height: 48px;
text-align: center; }
.page_head_memu .span_names{
font-size: 30px;
} .action-right{
position: absolute;
top: 5px;;
right: 10px;
height: 48px;
width: 100px;
}
.action-right a{
color: #3d3d3d;
text-decoration: none;
} .action-right #img_head{
height: 40px;
width: 40px;
border-radius: 50%;
float: left;
font-size: 10px; }
.action-right #img_head:hover{
width: 50px;
height: 50px;
} .memu{
position: absolute;
width: 150px;
height: 550px;
top: 50px;
left: 20px;
bottom: 0px;
border: 1px solid #c0cddf;
overflow :auto
}
.memu .items{
display: block;
text-decoration: none;
padding: 5px 10px;
margin: 5px 10px; }
.new{
background-color: #2d2d2d;
}
.content{
position:absolute;
left: 175px;
right: 0;
top: 50px;
bottom:0;
background-color: azure; }
.menu .items.active{
background-color: black;
color: white;
} #mytable {
width: 1000px;
padding: 0;
margin: 0;
} caption {
padding: 0 0 5px 0;
width: 700px;
font: italic 20px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
text-align: right;
} th {
font: bold 20px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
color: #4f6b72;
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
border-top: 1px solid #C1DAD7;
letter-spacing: 2px;
text-transform: uppercase;
text-align: left;
padding: 6px 6px 6px 12px;
background: #CAE8EA no-repeat;
} th.nobg {
border-top: 0;
border-left: 0;
border-right: 1px solid #C1DAD7;
background: none;
} td {
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
background: #fff;
font-size:16px;
padding: 6px 6px 6px 12px;
color: #4f6b72;
} td.alt {
background: #F5FAFA;
color: #797268;
} th.spec {
border-left: 1px solid #C1DAD7;
border-top: 0;
background: #fff no-repeat;
font: bold 10px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
} th.specalt {
border-left: 1px solid #C1DAD7;
border-top: 0;
background: #f5fafa no-repeat;
font: bold 10px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
color: #797268;
} #mytable td:hover{
background-color: #C1DAD7;
} {% block css %} {% endblock %}
</style> </head> <body>
<div class="page_head">
<div class="page_head_memu">
<span class="span_names">后台管理</span>
</div>
<div class="action-right">
<a id="info" href="/logout"><img id="img_head" alt="传个头像吧" title="点击注销登录" src="/{{ img_list.0.path }}" /></a>
<a class="action-nav">{{ username }}</a>
<a href="#" class="action-nav username" style="float:left;display: none">注销</a><br>
<a style="font-size: 8px">版本:V1.0</a>
</div> </div> <div class="memu">
<a href="/classes/" class="items" id="memu_classes">班级管理</a>
<a href="/student/" class="items" id="memu_student">学生管理</a>
<a href="/teacher/" class="items" id="memu_teacher">教师管理</a>
</div>
<div class="content">
{% block content %}
<div style="width: 700px">
<form method="POST" action="" enctype="multipart/form-data" style="width: 600px">
<input type="file" name="fafafa" />
<input type="submit" value="提交" />
</form> <div>
{% for item in img_list %}
<img style="height: 40px;width: 40px;border-radius: 50%" src="/{{ item.path }}" />
{% endfor %}
</div> </div>
{% endblock %}
</div> {# <div class="page_foot"></div>#} <script src="/static/jquery-2.1.4.min.js"></script>
<!--<script>
$('.memu a').click(function(){
$(this).siblings().removeClass('new');
$(this).addClass('new');
})
</script>--> {% block js %} {% endblock %}
</body> </html>

主界面--包括注销登录部分

Django 后台管理 之登录和注销的更多相关文章

  1. Django——后台管理

    1.要使用Django-admin后台的前提 INSTALLED_APPS = [ 'simpleui', 'django.contrib.admin', #必须有这一项 'django.contri ...

  2. Shiro 整合SpringMVC 并实现权限管理,登录和注销

    Shiro 整合SpringMVC 并且实现权限管理,登录和注销 Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring S ...

  3. django后台管理-admin

    0922自我总结 django后台管理-admin 一.模型注册 admin.py 注册方式一: #在对于注册的app中的admin文件中导入模型然后注册模型 admin.site.register( ...

  4. Django后台管理的使用

    Django后台管理的使用 参考文章:https://www.runoob.com/django/django-admin-manage-tool.html 1.编写好models 直接在admin. ...

  5. django后台管理-ModelAdmin对象

    Django最强大的部分之一是自动生成的管理后台界面. 它从你的模型中读取元数据,以提供一个快速的.以模型为中心的界面,信任的用户可以在这里管理你网站上的内容. 建议管理后台仅作为组织的一个内部管理工 ...

  6. Django后台管理界面

    之前的几篇记录了模板视图.模型等页面展示的相关内容,这篇主要写一下后台admin管理界面的内容. 激活管理界面 Django管理站点完全是可选择的,之前我们是把这些功能给屏蔽掉了.记得上篇中Djang ...

  7. 强大的Django后台管理

    Django 后台 django的后台我们只要加少些代码,就可以实现强大的功能.与后台相关文件:每个app中的 admin.py 文件与后台相关 下面示例是做一个后台添加博客文章的例子: 新建一个 名 ...

  8. [oldboy-django][1初始django]后台管理页面的布局 + djano母版(继承html)

    完善学员管理系统 - bootstrap fontawesome - 分页,路径导航,表格(class样式),消息图标(i标签),邮件图标(i标签) - 响应式导航 @media(min-width, ...

  9. django后台管理

    后台管理 1)  本地化 语言和时区的本地化. 修改settings.py文件. # LANGUAGE_CODE = 'en-us' LANGUAGE_CODE = 'zh-hans' # TIME_ ...

随机推荐

  1. eclipse离线安装插件过程

    离线安装插件: 1. help -> install New Softe.. 2. 打开安装插件界面 最后点击,next, 同意事项,重启eclipse.

  2. linux中grep注意

    grep -l 只输出文件名: -h 只输出匹配的行 不输出文件名: -c 之处匹配内容的行数: -n 将结果输出的同时,也输出改行的行号: -c 统计查到的总行数: -i 忽略大小写: grep ' ...

  3. Mockito 库、powermock扩展

    转载:http://blog.csdn.net/kittyboy0001/article/details/18709685 Mockito 简介 Mockito 是目前 java 单测中使用比较流行的 ...

  4. 常见CSS两栏式布局

    代码下载:https://files.cnblogs.com/files/xiandedanteng/TwoColumnLayout.rar 效果展示: 代码: <!DOCTYPE html&g ...

  5. java 中 instanceof 和 isInstance区别

    两者的功能是等价的.区别: 1.instanceof 是一个操作符(类似new, ==等): 使用方法: if (ins instanceof String) { //logic } 2.isInst ...

  6. 《C陷阱与缺陷》学习笔记(一)

    前言和导读 "得心应手的工具在初学时的困难程度往往超过那些easy上手的工具."比較认同这句话. 我至今认为自己事实上还是个刚入了门的刚開始学习的人. 第一章 "词法&q ...

  7. jquery代码小片段

    1. 使用jQuery来切换样式表 //找出你希望切换的媒体类型(media-type),然后把href设置成新的样式表. $(‘link[media="screen"]‘).at ...

  8. MySQL系列:innodb源代码分析之线程并发同步机制

    innodb是一个多线程并发的存储引擎,内部的读写都是用多线程来实现的,所以innodb内部实现了一个比較高效的并发同步机制. innodb并没有直接使用系统提供的锁(latch)同步结构,而是对其进 ...

  9. Android Handler 异步消息处理机制的妙用 创建强大的图片载入类

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38476887 ,本文出自[张鸿洋的博客] 近期创建了一个群.方便大家交流,群号: ...

  10. 整合Kafka到Spark Streaming——代码示例和挑战

    作者Michael G. Noll是瑞士的一位工程师和研究员,效力于Verisign,是Verisign实验室的大规模数据分析基础设施(基础Hadoop)的技术主管.本文,Michael详细的演示了如 ...