Django 后台管理 之登录和注销
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 后台管理 之登录和注销的更多相关文章
- Django——后台管理
1.要使用Django-admin后台的前提 INSTALLED_APPS = [ 'simpleui', 'django.contrib.admin', #必须有这一项 'django.contri ...
- Shiro 整合SpringMVC 并实现权限管理,登录和注销
Shiro 整合SpringMVC 并且实现权限管理,登录和注销 Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring S ...
- django后台管理-admin
0922自我总结 django后台管理-admin 一.模型注册 admin.py 注册方式一: #在对于注册的app中的admin文件中导入模型然后注册模型 admin.site.register( ...
- Django后台管理的使用
Django后台管理的使用 参考文章:https://www.runoob.com/django/django-admin-manage-tool.html 1.编写好models 直接在admin. ...
- django后台管理-ModelAdmin对象
Django最强大的部分之一是自动生成的管理后台界面. 它从你的模型中读取元数据,以提供一个快速的.以模型为中心的界面,信任的用户可以在这里管理你网站上的内容. 建议管理后台仅作为组织的一个内部管理工 ...
- Django后台管理界面
之前的几篇记录了模板视图.模型等页面展示的相关内容,这篇主要写一下后台admin管理界面的内容. 激活管理界面 Django管理站点完全是可选择的,之前我们是把这些功能给屏蔽掉了.记得上篇中Djang ...
- 强大的Django后台管理
Django 后台 django的后台我们只要加少些代码,就可以实现强大的功能.与后台相关文件:每个app中的 admin.py 文件与后台相关 下面示例是做一个后台添加博客文章的例子: 新建一个 名 ...
- [oldboy-django][1初始django]后台管理页面的布局 + djano母版(继承html)
完善学员管理系统 - bootstrap fontawesome - 分页,路径导航,表格(class样式),消息图标(i标签),邮件图标(i标签) - 响应式导航 @media(min-width, ...
- django后台管理
后台管理 1) 本地化 语言和时区的本地化. 修改settings.py文件. # LANGUAGE_CODE = 'en-us' LANGUAGE_CODE = 'zh-hans' # TIME_ ...
随机推荐
- Map接口及其子类
Map接口操作的是一对对象,即二元偶对象,Map接口中的每一个元素都使用"key--value"的形式存储在集合中. SortedMap接口是排序接口,仅仅要是实现了此接口的子类, ...
- JAVA Eclipse ActivityManager Warning Activity not started, its current task has been brought to the front怎么办
Eclipse运行提示Activity not started,因为当前程序已经在运行,需要退出当前程序再测试
- 微信小程序 - 考试状态不同显示
未开考 .已交卷. 考试中 .考试结束 #ddd #f00 #ff0 默认禁用色 禁用的button仅有style起作用,四个状态,通过wx:if ... elif ... e ...
- hibernate 数据缓存
http://www.cnblogs.com/wean/archive/2012/05/16/2502724.html,
- JAVA学习第十四课(接口:implements及其基本应用)
接口: 我们知道抽象类中能够定义抽象方法,也能够定义非抽象方法.当一个抽象类中的方法都是抽象方法的时候,我们就能够定义还有一种表现方式:接口(interface),所以接口是一种特殊的抽象类 接口的出 ...
- Cloudera Manager 和 CDH 4 终极安装
转载请注明出处:http://www.cnblogs.com/thinkCoding/p/3567408.html 系统环境 操作系统:CentOS 6.5 Cloudera Manager 版本:4 ...
- hdu 2602 - Bone Collector(01背包)解题报告
Bone Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- RGB颜色空间alpha混合的方法
http://blog.csdn.net/xhhjin/article/details/6444782http://blog.csdn.net/xhhjin/article/details/64454 ...
- 给定一个递增序列,a1 <a2 <...<an 。定义这个序列的最大间隔为d=max{ai+1 - ai }(1≤i<n),现在要从a2 ,a3 ..an-1 中删除一个元素。问剩余序列的最大间隔最小是多少?
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<vector> ...
- Git分支中的远程操作实践
Git分支中的远程操作实践 前几篇博客陆陆续续的讲了好多关于Git操作的内容, 其中在上篇博客聊了<Git中的merge.rebase.cherry-pick以及交互式rebase>,本篇 ...