用户名密码登陆实现:
在apps.users下找到views.py文件:
以下代码重写了authenticate()方法,方便用户扩展功能,比如邮箱验证登陆等。
在setting.py中重载一个变量:
AUTHENTICATION_BACKENDS = ('users.views.CustomBackend',)
from django.contrib.auth import authenticate, login
from django.contrib.auth.backends import ModelBackend
from django.db.models import Q
# 继承View 实现基于类的用户登陆
from django.views.generic.base import View
from .models import UserProfile
# 重写 authenticate 登陆验证方法
class CustomBackend(ModelBackend):
def authenticate(self, username=None, password=None, **kwargs):
try:
# 验证用户名或邮箱, Q提供了一个对象间的或(与&)运算
user=UserProfile
Django用户登录功能实现

本文介绍了如何在Django框架下实现用户名密码登录功能。通过修改views.py中的authenticate方法以支持扩展,如邮箱验证,同时在settings.py中进行配置。此外,文章还提及了使用POST请求处理用户登录验证,以及通过form.py定义表单验证规则,以减少无效提交和服务器负载,html页面则负责显示错误提示信息。
最低0.47元/天 解锁文章
920

被折叠的 条评论
为什么被折叠?



