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 小时前
Google ADK 学习指南
python·llm·agent·多agent·google adk·llmagent·agent cli
郝学胜-神的一滴1 小时前
Effective Python 条款35:用类封装替代生成器异常注入
开发语言·人工智能·python·金融·pandas·scikit-learn·matplotlib
LK_072 小时前
【力扣】142. 环形链表 II |Python
python·leetcode·链表
wuyk5552 小时前
Python办公自动化-第02讲-Excel文件认知与四大库选型
开发语言·python·excel
用户54817845909503 小时前
Java 与 Python 在大模型与智能体领域的选型
java·python
yiangL_3 小时前
1、Python 输入输出实验|print 与 input 人机交互实践
开发语言·python·人机交互
高洁014 小时前
世界模型的因果内核:从预测表象到理解机制
人工智能·python·深度学习·transformer·迁移学习
数字融合4 小时前
透明化视频动态三维重建空间数据零核心技术
人工智能·python·virtualenv
AI创界者5 小时前
Python 进阶实战(五十四章):大模型推理网关终极架构——分布式全局前缀 Trie 缓存同步与多网关高可用协同调度
人工智能·python·aigc