博主最近想基于Django框架开发一个测试平台,第一版先实现查看下载自动化的测试报告文件

第一步:前端框架

  网上选择一款开源boostrap的前端框架 AdminLTE,这里给个链接  https://adminlte.io/

第二步:实现登录/注销功能

  需要加入对登录状态的校验,准备引入session

1、base.html  基于开源框架:

 <!DOCTYPE html>
<html lang="zh-cn"> <head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>xx</title>
<!-- Tell the browser to be responsive to screen width -->
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<link rel="stylesheet" href="/static/bower_components/bootstrap/dist/css/bootstrap.min.css">
<!-- Font Awesome -->
<link rel="stylesheet" href="/static/bower_components/font-awesome/css/font-awesome.min.css">
<!-- Ionicons -->
<link rel="stylesheet" href="/static/bower_components/Ionicons/css/ionicons.min.css">
<!-- Theme style -->
<link rel="stylesheet" href="/static/dist/css/AdminLTE.min.css">
<link rel="stylesheet" href="/static/dist/css/skins/skin-blue.min.css">
<link rel="icon" href="/static/icon/icon-a.png"> <!-- Google Font -->
<link rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic">
</head> {% block starter %}
{% endblock %} {% block login %}
{% endblock %} <script src="/static/bower_components/jquery/dist/jquery.min.js"></script>
<!-- Bootstrap 3.3.7 -->
<script src="/static/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- AdminLTE App -->
<script src="/static/dist/js/adminlte.min.js"></script> <!-- Optionally, you can add Slimscroll and FastClick plugins.
Both of these plugins are recommended to enhance the
user experience. -->
</html>

2、登录页面

 {% extends 'base.html' %}
{% block login %}
<body class="hold-transition login-page">
<div class="login-box">
<div class="login-logo">
<a href="/login"><b>自动化</b>平台</a>
</div>
<!-- /.login-logo -->
<div class="login-box-body">
<p class="login-box-msg">输入用户邮箱与密码</p> <form method="post">
{% csrf_token %}
<div class="form-group has-feedback">
{{ uf.email }}
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
{{ uf.password }}
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-12">
<button type="submit" class="btn btn-primary btn-block btn-flat">登录</button>
</div>
<!-- /.col -->
</div>
</form> </div>
<!-- /.login-box-body -->
</div>
</div>
{% endblock %}

3、首页

 {% extends 'base.html' %}
{% block starter %}
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
<!-- Main Header -->
<header class="main-header"> <!-- Logo -->
<a href="/index" class="logo">
<!-- mini logo for sidebar mini 50x50 pixels -->
<span class="logo-mini"><b>自</span>
<!-- logo for regular state and mobile devices -->
<span class="logo-lg"><b>自</b>动化平台</span>
</a> <!-- Header Navbar -->
<nav class="navbar navbar-static-top" role="navigation">
<!-- Sidebar toggle button-->
<a href="#" class="sidebar-toggle" data-toggle="push-menu" role="button">
<span class="sr-only">Toggle navigation</span>
</a>
<!-- Navbar Right Menu -->
<div class="navbar-custom-menu">
<ul class="nav navbar-nav">
<!-- User Account Menu -->
<li class="dropdown user user-menu">
<!-- Menu Toggle Button -->
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<!-- hidden-xs hides the username on small devices so only the image appears. -->
<i class="fa fa-user"></i>
<span class="hidden-xs">{{ username }}</span>
</a>
</li>
<li>
<a href="/logout">
<i class="fa fa-sign-out"></i>
<span class="hidden-xs">登出</span>
</a>
</li>
<li>
<a href="/admin">
<i class="fa fa-gears"></i>
<span class="hidden-xs">后台</span>
</a>
</li>
</ul>
</div>
</nav>
</header>
<!-- Left side column. contains the logo and sidebar -->
<aside class="main-sidebar"> <!-- sidebar: style can be found in sidebar.less -->
<section class="sidebar"> <!-- Sidebar Menu -->
<ul class="sidebar-menu" data-widget="tree">
<li class="header">菜单</li>
<!-- Optionally, you can add icons to the links -->
<li class="active">
<a href="#"><i class="fa fa-link"></i> <span>模板</span></a>
</li>
<li class="treeview">
<a href="#"><i class="fa fa-link"></i> <span>接口模块</span>
<span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i>
</span>
</a>
<ul class="treeview-menu">
{% for path in paths %}
<li><a href="/paths/{{ path.path_id }}">{{ path.path }}</a></li>
{% endfor %}
</ul>
</li>
<li>
<a href="xx"><i class="fa fa-link"></i> <span>禅道</span></a>
</li>
<li>
<a href="xx"><i class="fa fa-link"></i> <span>Jenkins</span></a>
</li>
</ul>
<!-- /.sidebar-menu -->
</section>
<!-- /.sidebar -->
</aside> <!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
{% block paths %}
{% endblock %}
</div>
<!-- /.content-wrapper --> <!-- Control Sidebar -->
<aside class="control-sidebar control-sidebar-dark">
<!-- Create the tabs -->
<ul class="nav nav-tabs nav-justified control-sidebar-tabs">
<li class="active"><a href="#control-sidebar-home-tab" data-toggle="tab"><i class="fa fa-home"></i></a></li>
<li><a href="#control-sidebar-settings-tab" data-toggle="tab"><i class="fa fa-gears"></i></a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<!-- Home tab content -->
<div class="tab-pane active" id="control-sidebar-home-tab">
<h3 class="control-sidebar-heading">Recent Activity</h3>
<ul class="control-sidebar-menu">
<li>
<a href="javascript:;">
<i class="menu-icon fa fa-birthday-cake bg-red"></i> <div class="menu-info">
<h4 class="control-sidebar-subheading">Langdon's Birthday</h4> <p>Will be 23 on April 24th</p>
</div>
</a>
</li>
</ul>
<!-- /.control-sidebar-menu --> <h3 class="control-sidebar-heading">Tasks Progress</h3>
<ul class="control-sidebar-menu">
<li>
<a href="javascript:;">
<h4 class="control-sidebar-subheading">
Custom Template Design
<span class="pull-right-container">
<span class="label label-danger pull-right">70%</span>
</span>
</h4> <div class="progress progress-xxs">
<div class="progress-bar progress-bar-danger" style="width: 70%"></div>
</div>
</a>
</li>
</ul>
<!-- /.control-sidebar-menu --> </div>
<!-- /.tab-pane -->
<!-- Stats tab content -->
<div class="tab-pane" id="control-sidebar-stats-tab">Stats Tab Content</div>
<!-- /.tab-pane -->
<!-- Settings tab content -->
<div class="tab-pane" id="control-sidebar-settings-tab">
<form method="post">
<h3 class="control-sidebar-heading">General Settings</h3> <div class="form-group">
<label class="control-sidebar-subheading">
Report panel usage
<input type="checkbox" class="pull-right" checked>
</label> <p>
Some information about this general settings option
</p>
</div>
<!-- /.form-group -->
</form>
</div>
<!-- /.tab-pane -->
</div>
</aside>
<!-- /.control-sidebar -->
<!-- Add the sidebar's background. This div must be placed
immediately after the control sidebar -->
<div class="control-sidebar-bg"></div>
</body>
{% endblock %}

4、views.py

 # -*- coding: utf-8 -*-
from __future__ import unicode_literals from django.shortcuts import render, render_to_response
from django.http import HttpResponse, HttpResponseRedirect, StreamingHttpResponse
from django.views.decorators.csrf import csrf_exempt
from django.template import RequestContext
from models import User, Path, File
from form import UserForm import random # Create your views here. # 基础页面
def base(request):
return render(request, "base.html") # 开始页面
def starter(request):
return render(request, "starter.html") # 首页
def index(request):
# 判断是否有session
username = request.session.get("username")
if username:
report_paths = Path.objects.all()
paths = []
for x, y in enumerate(report_paths):
path = {
"path_id": x + 1,
"path": y.path
}
paths.append(path)
return render(request, "index.html", {"paths": paths,
"username": username})
else:
# 如果没有session,重定向到路由 /login/, 返回表单
uf = UserForm(request.POST)
return HttpResponseRedirect("/login/", {"uf": uf}) # 登录
@csrf_exempt
def login(request):
if request.method == "POST":
uf = UserForm(request.POST)
if uf.is_valid():
email = uf.cleaned_data["email"]
password = uf.cleaned_data["password"]
user = User.objects.filter(email=email, password=password)
# print user[0].username
if user:
request.session["username"] = user[0].username
# 校验通过,重定向到/index/
return HttpResponseRedirect("/index/")
else:
uf = UserForm(request.POST)
return render(request, "login.html", {"uf": uf})
else:
uf = UserForm(request.POST)
return render(request, "login.html", {"uf": uf}) # 注销
def logout(request):
del request.session["username"] # 删除session
uf = UserForm(request.POST)
return HttpResponseRedirect("/login/", {"uf": uf})

5、启动进入登录页面

6、登录成功后:

代码中加了一部分的注释、继续完善平台功能

Django框架基于session的登录/注销实现的更多相关文章

  1. 第三百一十三节,Django框架,Session

    第三百一十三节,Django框架,Session Django中默认支持Session,其内部提供了5种类型的Session供开发者使用: 1.数据库(默认)2.缓存3.文件4.缓存+数据库5.加密c ...

  2. python测试开发django-43.session机制(登录/注销)

    前言 当我们登录访问一个网站时,服务器需要识别到你已经登录了,才有相应的权限访问登录之后的页面.用户退出登录后,将无权限访问再访问登录后的页面. 从登录到退出的一整个流程,可以看成是与服务器的一次会话 ...

  3. django框架--cookie/session

    目录 一.http协议无状态问题 二.会话跟踪技术--cookie 1.对cookie的理解 2.cookie的使用接口 3.cookie的属性 4.使用cookie的问题 三.会话跟踪技术--ses ...

  4. 十一 Django框架,Session

    Django中默认支持Session,其内部提供了5种类型的Session供开发者使用: 1.数据库(默认)2.缓存3.文件4.缓存+数据库5.加密cookie 1.数据库Session,保存在数据库 ...

  5. Django基础之Session版登录验证

    from functools import wraps def check_login(func): @wraps(func) def inner(request, *args, **kwargs): ...

  6. 关于在django框架里取已登录用户名字的问题

    在 views里取值是这样的 request.user.username而在模板页面取值是这样的:{{request.user}} 判断是否通过验证是这样的 {% if request.user.is ...

  7. Django框架中session存储到redis中的配置

    本文链接:https://blog.csdn.net/linqunbin/article/details/94786313————————————————版权声明:本文为CSDN博主「linqunbi ...

  8. Django框架之cookie和session及开发登录功能

    1.cookie是什么? Web应用程序是使用HTTP协议传输数据的.HTTP协议是无状态的协议.一旦数据交换完毕,客户端与服务器端的连接就会关闭,再次交换数据需要建立新的连接.这就意味着服务器无法从 ...

  9. Django框架详细介绍---cookie、session、自定义分页

    1.cookie 在HTTP协议介绍中提到,该协议是无状态的,也就是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不会直接影响后面的 ...

随机推荐

  1. 【JavaScript算法】---希尔排序

    一.什么是希尔排序 希尔排序(Shell's Sort)是插入排序的一种又称“缩小增量排序”,是直接插入排序算法的一种更高效的改进版本.   思路:      希尔排序是把记录按下标的一定增量分组,对 ...

  2. spring boot实战(第一篇)第一个案例

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   spring boot实战(第一篇)第一个案例 前言 写在前面的话 一直想将spring boot相关内容写成一个系列的 ...

  3. 如何在不改SQL的情况下优化数据库

    主题简介 在数据库运维中我们会遇到各种各样的问题,这些问题的根源可能很明显,也可能被某种表象掩盖而使我们认不清.所以运维面临的两大问题就是,第一我们没有看清本质,第二应用不允许修改.那么我们如何解决这 ...

  4. kitkat-s5p4418drone 记录

      查看帮助: ./device/nexell/tools/build.sh  -h   编译u-boot: ./device/nexell/tools/build.sh -b drone2 -t u ...

  5. 怎么将Android的API demo导入到Eclipse工作区中

    File ->New Android Project 选择Create project from existing sample (不同Android版本有对应的ApiDemo示例).

  6. SQLAlchemy中scoped_session实现线程安全

    不多说,先上代码 from sqlalchemy.orm import sessionmaker from sqlalchemy import create_engine from sqlalchem ...

  7. Romantic---hdu2669(扩展欧几里德模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2669 详解:扩展欧几里德 #include <iostream> #include < ...

  8. 双态运维分享之:业务场景驱动的服务型CMDB

    最近这几年,国内外CMDB失败的案例比比皆是,成功的寥寥可数,有人质疑CMDB is dead?但各种业务场景表明,当下数据中心运维,CMDB依然是不可或缺的一部分,它承载着运维的基础,掌握运维的命脉 ...

  9. 使用linuxbridge + vlan网络模式

    #openstack pike 使用 linuxbridge + vlan openstack pike 集群高可用  安装部署 汇总 http://www.cnblogs.com/elvi/p/76 ...

  10. day08:软件系统的体系结构&Tomcat详解&Web应用&http协议

        day08 软件系统体系结构     常见软件系统体系结构B/S.C/S 1.1 C/S C/S结构即客户端/服务器(Client/Server),例如QQ: 需要编写服务器端程序,以及客户端 ...