基于控制权限和登录验证跳转的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数据库,角色设置 ...
随机推荐
- 【实验 1-1】编写一个简单的 TCP 服务器和 TCP 客户端程序。程序均为控制台程序窗口。
在新建的 C++源文件中编写如下代码. 1.TCP 服务器端#include<winsock2.h> //包含头文件#include<stdio.h>#include<w ...
- i2c sub system __i2c_board_list/klist_devices/klist_drivers
i2c_devinfo全局链表: __i2c_board_list 用来挂接 i2c_board_info,这个信息用来生成 i2c_client i2c_client 链表: i2c_bus_typ ...
- Python:常见错误集锦(持续更新ing)
初学Python,很容易与各种错误不断的遭遇.通过集锦,可以快速的找到错误的原因和解决方法. 1.IndentationError:expected an indented block 说明此处需要缩 ...
- java基础之导入(药师点评)
/** * 药师点评的导入 * @param request * @param response * @param f * @param tmallTcMessageImport * @return ...
- 网页JavaScript2
window.close() 关闭网页, window.opener.close() 关闭打开当前窗口的源窗口 间隔与延迟 window.setlnterval("执行代码&q ...
- web笔记
application: 在tomcat启动过程,会将所有的应用加载进来,会为每一个应用创建一个application对象.这个对象是唯一.但是所有的web应用是互不影响的. like模糊查询 重定向 ...
- 网站项目后台的目录命名为admin后,网页莫名其妙的变样了
这是我的第一篇博客文章,与其说是分享经验,倒不如说是求助 最近因为要完成一个课程设计,在拿一个现成的项目过来改,要用到select下拉菜单,可是发觉怎么我的这个下拉菜单怎么变样了 刚开始它是这样的 感 ...
- python 登陆一个网站
今天想用python写一个登陆的脚本,搜了一下,网上挺多的,看了一些后写了个登陆虎扑论坛的脚本. 原理: 只要在发送http请求时,带上含有正常登陆的cookie就可以了. 1.首先我们要先了解coo ...
- 关于js中的事件
这是第一篇技术性博客. 因为最近做的web版前端求职简历算是告一段落了(点此看简历).(稍微记录下吧:自从确定简历的简笔画风格后(因为刚开始设想的蓝天白云大树啥的不仅图片特难找而且做着做着就觉得有点俗 ...
- 关于 HRESULT:0x80070
异常来自 HRESULT:0x80070057 (E_INVALIDARG) 网上看的普遍办法是: 解决方法 是 删除 C:/WINDOWS/Microsoft.NET/Framework/v2.0. ...