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': '验证码错误'})
相关推荐
喝茶与编码6 分钟前
真实业务场景:高并发 Upsert 死锁频发?InnoDB 锁机制与重试机制深度解析
数据库·python
倒流时光三十年23 分钟前
第一阶段 05 · Java 客户端查询类详解(Query / SearchCriteria / Response 与复杂拼接)
java·开发语言·python
CTA终结者36 分钟前
近期AI量化学习,把规则改写接到策略开发
人工智能·python
有Li38 分钟前
使用整合电子健康记录的大语言模型智能体实现前列腺癌患者教育个性化文献速递/医学智能体前沿
人工智能·python·机器学习·语言模型·医学生
米码收割机1 小时前
【Python】Flask+SQLite_web 宠物领养系统 (源码+文档)【独一无二】
前端·python·flask
卷无止境1 小时前
python进阶:类方法与静态方法的分野,classmethod 和 staticmethod 到底该怎么选
后端·python
卷无止境2 小时前
继承与多态:Python OOP里最容易被用错的两把梯子
后端·python
SelectDB技术团队2 小时前
丰巢日志平台 ELK 替代:Apache Doris / SelectDB 的技术能力与实践
开发语言·python
崖边看雾2 小时前
Python学习——函数
开发语言·windows·python·学习·pycharm