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': '验证码错误'})
相关推荐
用户8356290780515 小时前
Python 实现 PDF 文件加密与解密方法
后端·python
用户8356290780515 小时前
使用 Python 冻结与拆分 Excel 窗格教程
后端·python
你好潘先生13 小时前
别再记命令了,用 yeero do 说句人话就能跑脚本,而且不烧 token
服务器·python·命令行
Agent_大师13 小时前
WebSocket 行情重连成功,K线缺口不会自动消失
python
荣码13 小时前
LLM结构化输出:让AI返回JSON而不是废话,我踩了4个坑
java·python
copyer_xyf14 小时前
FastAPI 如何连接 MySQL
后端·python
apocelipes1 天前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
用户8356290780511 天前
使用 Python 在 PDF 中创建与管理书签
后端·python
MeixianAgent1 天前
Python 回测数据入口怎么验?历史 K 线入库前先做 5 个检查
后端·python
咕白m6252 天前
用 Python 实现一键批量查找与替换 Excel 数据
后端·python