支持邮箱/手机号/昵称登录,在django1.6.2测试成功。
1、models

# -*- encoding: utf-8 -*-
from django.db import models
from django.contrib.auth.models import AbstractUser from common.thumbs import ImageWithThumbsField class User(AbstractUser):
avatar = ImageWithThumbsField('头像', max_length=200, blank=True, null=True, upload_to='avatar/%Y/%m/%d/%H/%M%S', sizes=((30, 30), (50, 50), (100, 100), (180, 180), ))
mobile = models.CharField(max_length=100, null=True, blank=True, db_index=True)

2、自定义登陆验证后台

#coding=utf-8
'''
自定义登陆验证后台 Created on 2014年3月31日 @author: linjiqin
'''
import re from accounts.models import User class LoginBackend(object):
def authenticate(self, username=None, password=None):
if username:
#email
if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", username) != None:
try:
user = User.objects.get(email=username)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
#mobile
elif len(username)==11 and re.match("^(1[3458]\d{9})$", username) != None:
try:
user = User.objects.get(mobile=username)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
#nick
else:
try:
user = User.objects.get(username=username)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
else:
return None def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None

3、settings.py中添加自定义验证后台

AUTHENTICATION_BACKENDS = (
'accounts.backends.LoginBackend',
)

4、在视图中使用自定义后台验证

# -*- encoding: utf-8 -*-
from django.conf import settings
from django.contrib import auth
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.utils import simplejson
from django.views.decorators.csrf import csrf_exempt, csrf_protect
from django.views.decorators.cache import never_cache
from django.views.decorators.http import require_POST @require_POST
def login(request):
username = request.POST['username']
password = request.POST['password'] result = {"status": False, "data":""} if username=="" or username.isspace():
result = {"status": False, "data":"用户名不能为空"}
return HttpResponse(simplejson.dumps(result, ensure_ascii = False), mimetype="application/json")
if password=="" or password.isspace():
result = {"status": False, "data":"密码不能为空"}
return HttpResponse(simplejson.dumps(result, ensure_ascii = False), mimetype="application/json") user = auth.authenticate(username=username, password=password)
if user is not None:
if user.is_active:
auth.login(request, user)
result = {"status": True, "data":"OK"}
return HttpResponse(simplejson.dumps(result, ensure_ascii = False), mimetype="application/json")
else:
result = {"status": False, "data":"["+username+"]已被暂时禁用"}
return HttpResponse(simplejson.dumps(result, ensure_ascii = False), mimetype="application/json")
else:
result = {"status": False, "data":"用户名或密码不正确,请重试"}
return HttpResponse(simplejson.dumps(result, ensure_ascii = False), mimetype="application/json")

Django自定义登陆验证后台的更多相关文章

  1. django实现自定义登陆验证

    django实现自定义登陆验证 自定义装饰器函数和类 from utils.http import HttpResponseUnauthorized from django.views import ...

  2. Django自定义User模型和登录验证

    用户表已存在(与其他App共用),不能再使用Django内置的User模型和默认的登录认证.但是还想使用Django的认证框架(真的很方便啊). 两个步骤: 1)自定义Use模型,为了区分系统的Use ...

  3. Django之Cookie Session详解,CBV,FBV登陆验证装饰器和自定义分页

    Cookie Session和自定义分页   cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接 ...

  4. django 自定义 密码加密方式 及自定义验证方式

    在django1.6中,默认的加密方式是pbkdf_sha256,具体算法不表,一直以来用django的自带用户验证都十分顺手,但如果需要修改默认加密方式为md5,具体方法为: 在settings.p ...

  5. Django项目:CRM(客户关系管理系统)--38--30PerfectCRM实现King_admin编辑自定义字段验证

    # kingadmin.py # ————————04PerfectCRM实现King_admin注册功能———————— from crm import models #print("ki ...

  6. Django用户登陆以及跳转后台管理页面3

    Django用户登陆以及跳转后台管理页面1http://www.cnblogs.com/ujq3/p/7891774.html Django用户登陆以及跳转后台管理页面2http://www.cnbl ...

  7. Django用户登陆以及跳转后台管理页面2

    请先写好以下,再来替换文件 Django用户登陆以及跳转后台管理页面1http://www.cnblogs.com/ujq3/p/7891774.html from django.shortcuts ...

  8. django自定义实现登录验证-更新版

    django自定义实现登录验证 django内置的登录验证必须让开发者使用django内置的User模块,这会让开发者再某些方面被限制住 下面的模块是我自己自定义实现的django验证,使用方式和dj ...

  9. Django中间件(中间件版登陆验证、访问频率限制)

    一.介绍 官方的说法:中间件是一个用来处理Django的请求和响应的框架级别的钩子.它是一个轻量.低级别的插件系统,用于在全局范围内改变Django的输入和输出.每个中间件组件都负责做一些特定的功能. ...

随机推荐

  1. 阿里云RDS外网无法访问解决办法

    为了安全起见,阿里云的RDS数据库设置只能内网连接,那么为了方便查询数据,我每次都去连接内网服务器远程桌面. 这次我因为网络问题,远程桌面非常不稳定,一会掉线一会掉线,只能另想办法. 同事那里有个批处 ...

  2. retain, copy, assign区别

    1.retain, copy, assign区别 假设你用malloc分配了一块内存,并且把它的地址赋值给了指针a,后来你希望指针b也共享这块内存,于是你又把a赋值给(assign)了b.此时a 和b ...

  3. vi/vim使用总结

    第一部份:一般模式可用的按钮说明,光标移动.复制粘贴.搜索取代等 移动光标的方法: h 或 向左箭头键(←) 光标向左移动一个字符 j 或 向下箭头键(↓) 光标向下移劢一个字符 k 或 向上箭头键( ...

  4. 757. Set Intersection Size At Least Two

    An integer interval [a, b] (for integers a < b) is a set of all consecutive integers from a to b, ...

  5. django中如何建立抽象型数据库作为父模块可继承其功能

    先建立抽象数据库 from django.db import models class BaseModel(models.Model): """为模型类补充字段" ...

  6. selenium定位元素提示‘元素不可见’问题解决方法

    最近在使用selenium的过程中发现有元素能够在页面中查找到,但是pycharm中运行时始终报错element not visible,于是使用如下方法成功解决问题. 1.driver.find_e ...

  7. Reviewing notes 2.1 of Mathematical analysis

    Chapter2 Numerical sequence and function Cartesian product set If S and T are sets,then the cartesia ...

  8. HDU - 5996 树上博弈 BestCoder Round #90

    就是阶梯NIM博弈,那么看层数是不是奇数的异或就行了: #include<iostream> #include<cstdio> #include<algorithm> ...

  9. CentOS 6.* 安装node

    CentOS6 安装node 使用node -v 是报错 ./node: error while loading shared libraries: libstdc++.so.6: cannot op ...

  10. class __init__()

    python 先定义函数才能调用 类是客观对象在人脑中的主观映射,生产对象的模板,类相当于盖房的图纸,生产工具的模具 模板 类:属性.方法 __init__() 这个方法一般用于初始化一个类但是 当实 ...