Django 后台管理 之登录和注销

时间:2022-02-15 00:29:48

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后台管理-ModelAdmin对象

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

  5. Django后台管理界面

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

  6. 强大的Django后台管理

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

  7. &lbrack;oldboy-django&rsqb;&lbrack;1初始django&rsqb;后台管理页面的布局 &plus; djano母版&lpar;继承html&rpar;

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

  8. django后台管理

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

  9. Django 后台管理xadmin

    一. xadmin的使用 后台管理在开发中可以给我们提供很大的便利,django自带了一个后台管理admin,不过还有一个xadmin比django自带的好用一些,功能更加强大,为模型提供了版本控制, ...

随机推荐

  1. ps你最容易忽略的知识

    了解更多ps知识 1. 快速打开文件­ 双击Photoshop的背景空白处(默认为灰色显示区域)即可打开选择文件的浏览窗口.­ 2. 随意更换画布颜色­ 选择油漆桶工具并按住Shift点击画布边缘,即 ...

  2. POJ 1724 ROADS【最短路&sol;搜索&sol;DP】

    一道写法多样的题,很具有启发性. 具体参考:http://www.cnblogs.com/scau20110726/archive/2013/04/28/3050178.html http://blo ...

  3. Educational Codeforces Round 6 D&period; Professor GukiZ and Two Arrays

    Professor GukiZ and Two Arrays 题意:两个长度在2000的-1e9~1e9的两个序列a,b(无序);要你最多两次交换元素,使得交换元素后两序列和的差值的绝对值最小:输出这 ...

  4. 【HDOJ】4587 TWO NODES

    Tarjan解无向图的割点和桥,参考白书. /* 4587 */ #include <iostream> #include <vector> #include <algo ...

  5. xcode中折叠打开代码

  6. Cocos2D iOS之旅&colon;如何写一个敲地鼠游戏&lpar;二&rpar;&colon;Cocos2D中的高清支持

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  7. HTML---标签的分类 &vert; display &vert; visibility

    一.HTML标签的分类和转换 1.1,三类HTML标签 1.2,三类HTML标签的特点 1.3,三类标签的转换--display:none隐藏于visibility不同之处 二.HTML某些标签--不 ...

  8. 为什么不同网段的ip 不能直接通信

    首先要明白一点,IOS一共七层, 发送数据的过程是从上到下,也就是从应用层一直到物理层,接受数据是从下至上. 来看你的问题,环境如下,我们来用一个ping命令的过程来解释:一个交换机,连两个电脑A和B ...

  9. cocos2d-x 初探helloWorld

    cocos2d-x的main函数代码很少,把一些复杂的接口封装到AppDelegate类里了,“AppDelegate”从词意可以得出是app的代理类,而一些最早的场景都会在AppDelegate类里 ...

  10. 不同格式的ip 统一转成ip列表

    支持以下格式的ip地址: 192.168.1.0/24 192.168.1.1-23 192.168.1.123 代码如下: package finder; import java.net.InetA ...