利用django自带认证功能实现用户登录认证。

views.py

 # Create your views here.

 from django.shortcuts import render_to_response,render,get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.template.context import RequestContext
from django.contrib.auth.models import User
from django.contrib import auth from forms import LoginForm def login(request):
if request.method == 'GET':
form = LoginForm()
return render_to_response('login.html',RequestContext(request,{'form':form,}))
else:
form = LoginForm(request.POST)
if form.is_valid():
username = request.POST.get('username','')
password = request.POST.get('password','')
user = auth.authenticate(username=username,password=password)
if user is not None and user.is_active:
auth.login(request,user)
return render_to_response('index.html',RequestContext(request))
else:
return render_to_response('login.html',RequestContext(request,{'form':form,'password_is_wrong':True}))
else:
return render_to_response('login.html',RequestContext(request,{'form':form,})) @login_required
def logout(request):
auth.logout(request)
return HttpResponseRedirect("/login/") @login_required
def index(request):
return render_to_response('index.html')

froms.py

 #coding=utf-8
from django import forms
from django.contrib.auth.models import User class LoginForm(forms.Form):
username = forms.CharField(
required = True,
label="用户名",
error_messages={'required':'请输入用户名'},
widget=forms.TextInput(
attrs={
'placeholder': "用户名",
'class':'form-control'
}
)
) password = forms.CharField(
required=True,
label="密码",
error_messages={'required':'请输入密码'},
widget=forms.PasswordInput(
attrs={
'placeholder':"密码",
'class':'form-control'
}
),
) def clean(self):
if not self.is_valid():
raise forms.ValidationError("用户名和密码为必填项")
else:
cleaned_data = super(LoginForm,self).clean()

login.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>登录</title>
<script type="text/javascript" src="/static/bootstrap/js/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="/static/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/static/bootstrap/css/bootstrap-theme.min.css">
<script type="text/javascript" src="/static/bootstrap/js/bootstrap.min.js"></script>
<style type="text/css">
html,body { margin:0; padding:0; overflow:hidden; height:100%; }
#jz-login { margin:0 auto; border:1px solid #666; width:300px; }
</style>
<script type="text/javascript">
function makeItMiddle() {
document.getElementById('jz-login').style.marginTop = (document.getElementsByTagName('body')[0].offsetHeight - document.getElementById('jz-login').offsetHeight) / 2 + 'px';
}
window.onload = makeItMiddle;
window.onresize = makeItMiddle;
</script>
</head>
<body>
{% if password_is_wrong %}
<div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">×</button>
<h4>错误!</h4>
用户名或密码错误
</div>
{% endif %}
<div class="well" id="jz-login" style="margin:auto">
<h1>用户登录</h1>
<form class="form-horizontal" action="" method="post">
{% csrf_token %}
{{ form }}
<p>
</p>
<p class="form-actions">
<input type="submit" value="登录" class="btn btn-primary">
<a href="/contactme/"><input type="button" value="忘记密码" class="btn btn-danger"></a>
<a href="/contactme/"><input type="button" value="新员工?" class="btn btn-success"></a>
</p>
</form>
</div>
<script src="/static/bootstrap/js/jquery.min.js"></script>
<script src="/static/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>

django用户认证的更多相关文章

  1. [django]用户认证中只允许登陆用户访问(网页安全问题)

    当设计一个重要网页时,一般要求未从登陆界面访问的用户不能进入其他页面,那么需要如何设置呢? 如下 django中的url.py urlpatterns = [    url(r'^$', 'login ...

  2. Django 用户认证及OneToOneField

    Django 用户认证如果自己不想写 就可以用django自带的认证 首选导入模块 models.py #!/usr/bin/env python #_*_ coding:utf8 _*_ from ...

  3. django用户认证系统——拓展 User 模型

    Django 用户认证系统提供了一个内置的 User 对象,用于记录用户的用户名,密码等个人信息.对于 Django 内置的 User 模型, 仅包含以下一些主要的属性: username,即用户名 ...

  4. “Django用户认证系统”学习资料收集

    首推追梦人物——Django用户认证系统 待续……

  5. Django 2.0 学习(17):Django 用户认证(auth模块)

    Django 用户认证(auth模块) 一.认证登陆 在进行用户登陆验证的时候,如果是自己写代码,就必须要先查询数据库,看用户输入的用户名是否存在于数据库中:如果用户存在于数据库中,然后再验证用户输入 ...

  6. django用户认证系统——重置密码7

    当用户不小心忘记了密码时,网站需要提供让用户找回账户密码的功能.在示例项目中,我们将发送一封含有重置用户密码链接的邮件到用户注册时的邮箱,用户点击收到的链接就可以重置他的密码,下面是具体做法. 发送邮 ...

  7. django用户认证系统——修改密码6

    再此之前我们已经完成了用户登录.注册.注销等功能,接下来让我们继续为用户提供修改密码的功能.该功能 Django 的 auth 应用也已经为我们提供,过程几乎和之前的登录功能完全一样. 编写修改密码模 ...

  8. django用户认证系统——拓展 User 模型2

    Django 用户认证系统提供了一个内置的 User 对象,用于记录用户的用户名,密码等个人信息.对于 Django 内置的 User 模型, 仅包含以下一些主要的属性: username,即用户名 ...

  9. django用户认证系统——基本设置1

    网站提供登录.注册等用户认证功能是一个常见的需求.因此,Django 提供了一套功能完整的.灵活的.易于拓展的用户认证系统:django.contrib.auth.在本教程中,我将向你展示 auth ...

  10. Django——用户认证

    Django--用户认证 用户与Authentication(身份验证) Django 用户认证系统处理用户帐号,组,权限以及基于cookie的用户会话. 这个系统一般被称为 auth/auth (认 ...

随机推荐

  1. Hibernate关联映射 映射文件的配置

    一:多对一单向关联 首先我们必须创建两个实体类 例如:Dept类 public class Dept { private Integer deptNo; private String dName; p ...

  2. 深刻理解:C#中的委托、事件

    C#中的事件还真是有点绕啊,以前用JavaScript的我,理解起来还真是废了好大劲!刚开始还真有点想不明白为什么这么绕,想想和JS的区别,最后终于恍然大悟! C#中事件绕的根本原因: C#的方法,它 ...

  3. MSCRM CRM 获取PickList 字段值函数解决方案

    表单中有很多picklist字段 不想写链接stringmap代码: 实体ID查询方法: SELECT ObjectTypeCode from Entity  where  name='实体名称' 调 ...

  4. 十种MYSQL显错注入原理讲解(二)

    上一篇讲过,三种MYSQL显错注入原理.下面我继续讲解. 1.geometrycollection() and geometrycollection((select * from(select * f ...

  5. SharePoint如何将使列表不被爬网爬到。

    有一个项目,没有对表单进行严格的权限管理,虽然用户在自己的首页只能看到属于的单子,但是在搜索的时候,所有人的单子都能被搜到,所以客户造成了困惑. 那么问题来了,怎么让列表或者文档库不被爬网爬到. 有两 ...

  6. swift 学习笔记

    1. 数组中取出字符串的方法: 1)let string = "\arr[0]" 2) let string = String(stringInterpolationSegment ...

  7. iOS 获取设备唯一标示符的方法

    在开发中会遇到应用需要记录设备标示,即使应用卸载后再安装也可重新识别的情况,在这写一种实现方式--读取设备的UUID(Universally Unique Identifier)并通过KeyChain ...

  8. RadioButton与CheckBox

    笔者长期从事于数据库的开发,算了,不提当年了,因为一直用的是小语种(PowerBuilder),还是来说说这两个最常见的控件吧! RadioButton(单选)和CheckBox(多选) 先来看看继承 ...

  9. iOS开发:创建真机调试证书及描述文件

    iOS开发:创建真机调试证书及描述文件 关于苹果iOS开发,笔者也是从小白过来的,经历过各种困难和坑,其中就有关于开发证书,生产证书,in_house证书,add_Hoc证书申请过程中的问题,以及上架 ...

  10. iOS 升级HTTPS通过ATS你所要知道的

    由于苹果规定2017年1月1日以后,所有APP都要使用HTTPS进行网络请求,否则无法上架,因此研究了一下在iOS中使用HTTPS请求的实现.网上搜索了一些比较有用资料,大家可以参考下 苹果强制升级的 ...