基于控制权限和登录验证跳转的django登录界面的实现
django框架提供了出去登录和退出系统的login和logout的视图函数,本实现中使用系统自带的是视图函数。需要在settings.py,urls.py,views.py和模板文件等几个方面进行考虑。
settings.py
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.locale.LocaleMiddleware'
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
) # Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'website.wsgi.application' import os.path TEMPLATE_DIRS = (
'/liuzp/DataCrawl/djcode/website/templates',
'/liuzp/DataCrawl/djcode/website/css/bootstrap',
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
) INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
'django.contrib.admindocs',
)
urls.py
首先编写urls.py文件。如下所示,其中django.contrib.auth.views.login和django.contrib.auth.views.logout视图函数是django框架提供的。
from django.conf.urls import patterns, include, url
from website import views
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
from django.contrib.auth.views import login,logout admin.autodiscover() urlpatterns = patterns('',
#登陆
(r'^accounts/login/$',login), #退出
(r'^accounts/logout/$',logout), #主界面
(r'^main/$',views.main),
)
views.py
其次编写views.py文件。主要是编写/main/的视图函数。使用decorators.login_required的装饰件,用来控制非登录用户的访问页面的权限,并且登录成功后,可以回到需要访问的页面。
login_required()的用法是:首先如果用户没有登录,则重定向至settings.LOGIN_URL,并且在查询字符串中传递当前绝对路径;其次如果用户已经登录,那么就正常的执行视图函数。
#encoding=utf-8
from django.http import HttpResponse
from django.shortcuts import render_to_response
from dboperation.user import isuserexist
from users.models import User
from django.contrib.auth.decorators import login_required
from django.contrib.auth.views import login,logout import datetime @login_required(login_url='/accounts/login')
def main(request):
return render_to_response('main.html')
模板文件
模板文件需要和login视图文件相对应,所以需要在template文件夹下建立/registration/login.html文件,作为登录界面。本文中的登录界面使用bootstrap的样式包。
如果账号和密码正确,则登录成功,页面将会重定向到next指定的URL中。如果next没有提供,则使用{{ next|add:"/main/" }}将页面重定向到/main/的界面。
<html>
<link href="/css/bootstrap.css" rel='stylesheet' type='text/css' />
<link href="/css/main.css" rel='stylesheet' type='text/css'>
<body style='background-color:#CCFFFF'>
<div class='row'>
<div class='col-md-3'>
</div>
<div class='col-md-6'>
<div class='login_form'>
<div class='login_title'>
<span class='text-success'>NCTC数据抓取平台</span>
</div>
<form class="form-horizontal" role="form" action="/accounts/login/" method="post">
{% csrf_token %}
<div class="form-group">
{% if form.errors %}
<label for="message" class="col-sm-5 control-label" style='margin:-10px 0px 5px 30px;color:#990033;font-size:10px'>账户或者密码错误</label>
{% endif %}
</div>
<div class='form-group'>
<label for="username" class="col-sm-2 control-label" style='color:#330066'>账号</label>
<div class="col-sm-5">
<input class="form-control" name="username" id='username' placeholder="用户名">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label" style='color:#330066'>密码</label>
<div class="col-sm-5">
<input type="password" class="form-control" name="password" id='password' placeholder="密码">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-primary" style='color:white' value='登录'/>
{% if next %}
<input type='hidden' name='next' value='{{ next }}'>
{% else %}
<input type='hidden' name='next' value='{{ next|add:"/main/" }}'>
{% endif %}
</div>
</div>
</form>
</div>
</div>
</div>
</body>
Admin后台管理
基于控制权限和登录验证跳转的django登录界面的实现的更多相关文章
- springboot shiro和freemarker集成之权限控制完全参考手册(跳过认证,登录由三方验证,全网首发)
本文主要考虑单点登录场景,登录由其他系统负责,业务子系统只使用shiro进行菜单和功能权限校验,登录信息通过token从redis取得,这样登录验证和授权就相互解耦了. 用户.角色.权限进行集中式管理 ...
- 使用ajax登录验证,第一次点击登录按钮无反应,第二次点击才能正常运行。
问题描述: 使用ajax进行登录验证时,第一次点击登录按钮无反应,第二次点击才能进去. 解决方法: 原来的代码 <form action="" method="po ...
- 简单两步快速实现shiro的配置和使用,包含登录验证、角色验证、权限验证以及shiro登录注销流程(基于spring的方式,使用maven构建)
前言: shiro因为其简单.可靠.实现方便而成为现在最常用的安全框架,那么这篇文章除了会用简洁明了的方式讲一下基于spring的shiro详细配置和登录注销功能使用之外,也会根据惯例在文章最后总结一 ...
- 基于Ajax与用户认证系统的登录验证
一.登录页面 from django.contrib import admin from django.urls import path from blog import views urlpatte ...
- Asp.NetMVC利用LigerUI搭建一个简单的后台管理详解(函登录验证)
上一篇 Asp.Net 中Grid详解两种方法使用LigerUI加载数据库数据填充数据分页 了解了LigerUI 中Grid的基本用法 现在结合上一篇的内容做一个简单的后台管理,当然也有前台的页面 ...
- Linux登录验证机制、SSH Bruteforce Login学习
相关学习资料 http://files.cnblogs.com/LittleHann/linux%E4%B8%AD%E7%94%A8%E6%88%B7%E7%99%BB%E5%BD%95%E8%AE% ...
- python装饰器 & flask 通过装饰器 实现 单点登录验证
首先介绍装饰器,以下是一段标注了特殊输出的代码.用于帮助理解装饰器的调用过程. import time def Decorator_one(arg1): info = "\033[1;31; ...
- asp.net 如何配置authentication,完成基于表单的身份验证
步骤一: 在根目录下的web.config中加入: <system.web> <authentication mode="Forms"> ...
- MongoDB登录验证及用户管理
一.超级管理员创建及开启登录验证 如果MongoDB要开启登录验证,必须在开启登录验证之前先创建好超级管理员,否则无法登录数据库! 例如,创建一个超级管理员admin,关联给admin数据库,角色设置 ...
随机推荐
- js 获取10个不重复随机数
var arr1 = new Array(); var arr2 = new Array(); for(var i = 0; i<20; i++){ arr1.push(i); } for(va ...
- MAC安装SVNServer
MAC已经自带了SVN,所以,直接使用就好 1.创建svn repository svnadmin create /path/svn/pro //仓库位置,svn是svn的目录,pro是一个版本库的 ...
- swift 创建tableView 并实现协议
import UIKit class ViewController2: UIViewController,UITableViewDelegate,UITableViewDataSource{ ...
- android启动优化
############################################## # power on till android lock screen comes up # # get ...
- Socket tips: UDP Echo service - Server code
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/soc ...
- hive优化要点总结
个人认为总体两种思想: 1.让服务器尽可能的多做事情,榨干服务器资源,以最高系统吞吐量为目标 再好的硬件没有充分利用起来,都是白扯淡. 比如: (1) 启动一次job尽可能的多做事情,一个job能完 ...
- SURF特征
了解了SIFT特征后,来学习SURF特征. 虽说是SIFT的一个变种,可是跟SIFT还是有差别的 差别有例如以下: 1.尺度空间的构建(近似)不同. 2.同意尺度空间多层图像同一时候被处理 3.特征点 ...
- cocos2d-x anchorPoint
之前一直没有用过anchorPoint,也感觉用这个东西的地方相对比较少的,都是直接使用世界坐标来定位的. 但是,在现在这个项目中,却有同事使用了这个anchorPoint,使用是使用了,但是,在碰撞 ...
- ExifInterface 多媒体文件附加信息
简介 ExifInterface类主要描述多媒体文件比如JPG格式图片的一些附加信息,包括拍摄时的光圈.快门.白平衡.ISO.焦距.日期时间等各种和拍摄条件以及相机品牌.型号.色彩编码 ...
- Task与Thread间的区别
通过查找一些文章,得知,Task与Thread不可比.Task是为了利用多CPU多核的机制而将一个大任务不断分解成小任务,这些任务具体由哪一个线程或当前线程执行由OS来决定.如果你想自己控制由哪一个T ...