Django自定义登陆验证后台
支持邮箱/手机号/昵称登录,在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")
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
2012-04-01 二十九、oracle 触发器
2012-04-01 数据库设计三大范式