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': '验证码错误'})
相关推荐
SelectDB12 分钟前
云数仓月度账单总是超预算:Apache Doris 与 Snowflake 五类成本项拆解与对账脚本
python
迪菲赫尔曼18 分钟前
【基础篇 12】把环境与源码接起来:检查 GPU,再安装服务器上的项目
运维·服务器·开发语言·人工智能·python·yolo·目标检测
2601_9628857233 分钟前
2026 Python 股票数据接口选型指南(横评 + 决策树)
开发语言·python·区块链
龙腾AI白云1 小时前
大模型检索增强生成(RAG)的技术原理与架构设计
数据库·python·机器学习·知识图谱
fanruitian1 小时前
python 可视化 panda seaborn matplotlib.pyplot
开发语言·python·matplotlib
梦想不只是梦与想1 小时前
Python 中的 json 模块
python·json·对象转字符串
星河耀银海1 小时前
Python文件操作:read、readline、readlines读取方式对比
python·文件操作·面向对象·基础函数·教程学习
傅科摆 _ py2 小时前
DP,贪心,堆和栈,二分力扣复习
开发语言·python·算法
吴名氏.2 小时前
Java转AI Agent教程(第一周)让模型先跑起来
人工智能·python·ai·ai agent
第404号工位3 小时前
离职退电脑前别只点“清空回收站”!手把手教你彻底抹除凭证与数字痕迹
linux·python·架构