Django captcha 验证

1.安装模块

复制代码
pip install django-simple-captcha
pip install Pillow

2.在settings中,将captcha注册到app列表里

复制代码
# MxOnline/settings.py
INSTALLED_APPS = [
    # 图片登陆验证
    'captcha',
]

3.captcha需要在数据库中建立自己的数据表,所以需要执行migrate命令生成数据表:

复制代码
py manage.py migrate

4.添加url路由

根目录下的urls.py文件中增加captcha对应的网址:

复制代码
# MxOnline/urls.py
urlpatterns = [
    path('register', RegisterView.as_view(), name='register'),
    # 这是生成验证码的图片
    path('captcha/',include('captcha.urls')),
]

5.修改forms

复制代码
# users/forms.py
from captcha.fields import CaptchaField



class RegisterForm(EmailCheckMixin):
    """注册表单"""
 
    # 为生成的验证码图片,以及输入框
    captcha = CaptchaField(error_messages={'invalid': '验证码错误'})
相关推荐
月光船幽幽1 分钟前
四层公理框架驱动AI健康诊断
人工智能·python·科技·算法·安全
郝学胜-神的一滴24 分钟前
力扣 692:巧用小顶堆高效求解前K个高频单词
java·数据结构·python·程序人生·算法·leetcode·职场和发展
栈溢出的浪漫25 分钟前
Python单元测试框架覆盖率-Coverage
python·单元测试·工具·覆盖率·coverage
淼澄研学27 分钟前
Python构建AI应用:从环境配置到FastAPI部署的5个实操步骤
人工智能·python·fastapi
1570925113430 分钟前
优先队列:从Java源码到实战
开发语言·python
Python私教1 小时前
Django + AI 做智能工单系统:自动分类、重复合并、服务时限与人工审批实战
人工智能·python·django
qq_22589174661 小时前
2026 计算机专业毕业设计选题推荐|Python 数据分析 / 可视化 / 推荐 / 预测(持续更新)
python·数据分析·课程设计
cui_ruicheng1 小时前
Python数据分析(十一):数据可视化与 Matplotlib
python·信息可视化·数据分析·matplotlib
superrick1 小时前
MCP 协议深度解析:AI Agent 的工具集成标准
python