2018年的网站制作,湖北省随州市建设厅网站,商业网站推荐,恶意推广网站目录
1.安装 django-simple-captcha模块
2. 在INSTALLED_APPS设置中添加对其配置类的引用
3.迁移数据库
4.添加路由
5.在自定义的登录表单中添加验证码字段
6.在视图函数中验证验证码
7.在html模板中使用验证码 1.安装 django-simple-captcha模块 pip install django-si…目录
1.安装 django-simple-captcha模块
2. 在INSTALLED_APPS设置中添加对其配置类的引用
3.迁移数据库
4.添加路由
5.在自定义的登录表单中添加验证码字段
6.在视图函数中验证验证码
7.在html模板中使用验证码 1.安装 django-simple-captcha模块 pip install django-simple-captcha
2. 在INSTALLED_APPS设置中添加对其配置类的引用
INSTALLED_APPS [# 图形验证码captcha,
]
3.迁移数据库
manage.pyMysite makemigrations
manage.pyMysite migrate
4.添加路由
urlpatterns [ path(captcha,include(captcha.urls)),
]
5.在自定义的登录表单中添加验证码字段
class LoginForm(forms.Form):用户登录表单# 重写用户名字段username forms.CharField(label用户名,widgetforms.TextInput,requiredTrue)# 重写密码字段password forms.CharField(label密码,widgetforms.PasswordInput(render_valueTrue),requiredTrue)# 验证码captcha CaptchaField(label验证码,# required参数用于确定是否为必填项requiredTrue)
6.在视图函数中验证验证码
使用is_valid()函数验证
# 接受返回表单
form LoginForm(datarequest.POST)
# 验证验证码是否匹配
if form.is_valid():pass7.在html模板中使用验证码 div classform-grouplabel验证码/label{{ form.captcha }}/div