Django 学习:为窗体加上防机器人的验证机制(验证码功能)

简介:   这里我们使用 django-simple-captcha 模块,官方介绍如下:https://github.com/mbi/django-simple-captcha一键安装:pip install django-simple-captcha 在 setting.

  这里我们使用 django-simple-captcha 模块,官方介绍如下:https://github.com/mbi/django-simple-captcha

一键安装:

pip install django-simple-captcha

 

在 setting.py 中把 'captcha' 加到 INSTALLED_APP 的区块中

INSTALLED_APPS =# ...
    'captcha',
    # ... 

 

由于此模块会到数据库建立自己的数据表,因此要先执行数据库的 migrate 操作:

python manage.py migrate

 

在 urls.py 中加上这个模块对应的网址:

from django.urls import path, re_path, include

urlpatterns = [
    #...
    url(r'^captcha/', include('captcha.urls'),
    # ...
]

 

在窗体类中加上 CaptchaField 字段 :

from captcha.fields import CaptchaField

class PostForm(forms.ModelForm):
    captcha = CaptchaField() #CaptchaField 字段
    class Meta:
        model = models.Post
        fields = ['mood', 'nickname', 'message', 'del_pass']

    def __init__(self, *args, **kwargs):
        super(PostForm, self).__init__(*args, **kwargs)
        self.fields['mood'].label = '现在的心情'
        self.fields['nickname'].label = '您的昵称'
        self.fields['message'].label = '心情留言'
        self.fields['del_pass'].label = '设置密码'
        self.fields['captcha'].label = '请输入验证码'

 

运行结果如下:

 

相关文章
|
3月前
|
存储 安全 网络协议
Python 教程之 Django(9)对模型中的字段进行验证
Python 教程之 Django(9)对模型中的字段进行验证
30 0
Python 教程之 Django(9)对模型中的字段进行验证
|
3月前
|
自然语言处理 Kubernetes 机器人
基于OpenIM 实现聊天机器人功能
基于OpenIM 实现聊天机器人功能
107 0
|
3月前
|
传感器 机器人 定位技术
Python 机器人学习手册:6~10
Python 机器人学习手册:6~10
42 0
|
3月前
|
传感器 Ubuntu 机器人
Python 机器人学习手册:1~5
Python 机器人学习手册:1~5
180 0
|
6月前
|
JSON 算法 安全
Django JWT验证
Django JWT验证
121 0
|
7月前
|
存储 缓存 数据库
Django框架学习(四)
Django框架学习(四)
|
1月前
|
机器人
量化交易机器人系统开发详情源码/功能步骤/需求设计/稳定版
he development of a quantitative trading robot system involves multiple aspects, including strategy design, data processing, and transaction execution. The following is a detailed overview of the development strategy for a quantitative trading robot system:
|
2月前
|
存储 前端开发 JavaScript
Django教程第4章 | Web开发实战-三种验证码实现
手动生成验证码,自动生成验证码,滑动验证码。【2月更文挑战第24天】
41 0
Django教程第4章 | Web开发实战-三种验证码实现
|
2月前
|
传感器 数据采集 人工智能
植保机器人多功能性
植保机器人多功能性
41 2
|
4月前
|
调度 Apache 数据库
Django 学习教程-第一个 Django 应用
本章主要讲述了如何创建一个Django项目,以一个投票案例程序介绍了Django的请求和响应流程。
51 1