django框架提供了出去登录和退出系统的login和logout的视图函数,本实现中使用系统自带的是视图函数。需要在settings.py,urls.py,views.py和模板文件等几个方面进行考虑。

settings.py

首先确保相应的配置是正确的。
将djnago.contrib.auth和django.contrib.contenttype放到INSTALLED_APPS设置中。INSTALLED_APPS和MIDDLEWARE_CLASSES的配置如下图所示。
设置模板文件路径为/liuzp/DataCrawl/djcode/website/templates和/liuzp/DataCrawl/djcode/website/css/bootstrap。
设置完成后,执行python manage.py syncdb,在数据库中生成相应的数据表。
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登录界面的实现的更多相关文章

  1. springboot shiro和freemarker集成之权限控制完全参考手册(跳过认证,登录由三方验证,全网首发)

    本文主要考虑单点登录场景,登录由其他系统负责,业务子系统只使用shiro进行菜单和功能权限校验,登录信息通过token从redis取得,这样登录验证和授权就相互解耦了. 用户.角色.权限进行集中式管理 ...

  2. 使用ajax登录验证,第一次点击登录按钮无反应,第二次点击才能正常运行。

    问题描述: 使用ajax进行登录验证时,第一次点击登录按钮无反应,第二次点击才能进去. 解决方法: 原来的代码 <form action="" method="po ...

  3. 简单两步快速实现shiro的配置和使用,包含登录验证、角色验证、权限验证以及shiro登录注销流程(基于spring的方式,使用maven构建)

    前言: shiro因为其简单.可靠.实现方便而成为现在最常用的安全框架,那么这篇文章除了会用简洁明了的方式讲一下基于spring的shiro详细配置和登录注销功能使用之外,也会根据惯例在文章最后总结一 ...

  4. 基于Ajax与用户认证系统的登录验证

    一.登录页面 from django.contrib import admin from django.urls import path from blog import views urlpatte ...

  5. Asp.NetMVC利用LigerUI搭建一个简单的后台管理详解(函登录验证)

    上一篇 Asp.Net 中Grid详解两种方法使用LigerUI加载数据库数据填充数据分页  了解了LigerUI 中Grid的基本用法  现在结合上一篇的内容做一个简单的后台管理,当然也有前台的页面 ...

  6. 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% ...

  7. python装饰器 & flask 通过装饰器 实现 单点登录验证

    首先介绍装饰器,以下是一段标注了特殊输出的代码.用于帮助理解装饰器的调用过程. import time def Decorator_one(arg1): info = "\033[1;31; ...

  8. asp.net 如何配置authentication,完成基于表单的身份验证

    步骤一: 在根目录下的web.config中加入: <system.web> <authentication mode="Forms">           ...

  9. MongoDB登录验证及用户管理

    一.超级管理员创建及开启登录验证 如果MongoDB要开启登录验证,必须在开启登录验证之前先创建好超级管理员,否则无法登录数据库! 例如,创建一个超级管理员admin,关联给admin数据库,角色设置 ...

随机推荐

  1. 修改cmd的字体

    通常打开的cmd默认的字体比较小,字体只有宋体和新宋体两种,如果要修改,需要通过修改注册表才行. 打开regedit后,找到如下路径HKEY_LOCAL_MACHINE\SOFTWARE\Micros ...

  2. java Date获取 年月日时分秒

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  3. [Redux] Implementing combineReducers() from Scratch

    The combineReducers function we used in previous post: const todoApp = combineReducers({ todos, visi ...

  4. MVC三和,你能辨别它?

    上次我们聊的时间MVC,而之前我们学习过三层.那么我们不禁就要问,他们说的是一回事吗.他们有什么联系吗? 三层架构(3-tier application)通常意义上的三层架构就是将整个业务应用划分为: ...

  5. jquery之遍历展示title

    //遍历展示title {field:'couponsList',title:'优惠劵类型',width:250,align:'center',sortable:true, formatter:fun ...

  6. BOM操作写法实例

    浏览器相关信息 // 浏览器信息 navigator.userAgent // Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/ ...

  7. 《第一行代码》学习笔记35-服务Service(2)

    1.Android的UI线程不安全,想要更新应用程序里的UI元素,则须在主线程中进行,否则会出现异常. 2.Android不允许在子线程里进行UI操作,对于该情况,Android提供了一套异步消息处理 ...

  8. ORACLE 物化视图

    最近几天,我负责的P项目环境中提供给W系统的一个视图,由于查询逻辑复杂,数据量比较大,导致每次查询视图的时候,查询速度慢,效率低下,遭到了w系统人员的投诉.想了想,还是改成物化视图吧,用了物化视图,腰 ...

  9. innodb_flush_method参数解析

    innodb_flush_method这个参数控制着innodb数据文件及redo log的打开.刷写模式,对于这个参数,文档上是这样描述的:有三个值:fdatasync(默认),O_DSYNC,O_ ...

  10. Jquery实现日期格式化

    格式一:yyyy-MM-dd HH:mm:ss Date.prototype.format = function(format) { /* * eg:format="yyyy-MM-dd h ...