html的表单图形验证码怎么做,django中简单图形验证码实现
要實現django圖形驗證碼,可以使用簡單的captcha
一、安裝captcha
在Pycharm的terminal中,使用pip安裝第三方庫:
執行命令:
pip install django-simple-captcha
二、注冊captcha
在settings中,將‘captcha’注冊到app列表里:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'login',
'captcha',
]
三、captcha需要在數據庫中建立自己的數據表,所以需要執行migrate命令生成數據表:
migrate
四、添加路由,在urls.py中增加這一行
path('captcha/', include('captcha.urls'))
五、表單中添加對應代碼。
from captcha.fields import CaptchaField #添加這里
class UserForm(forms.Form):
username =forms.CharField(label='用戶名',max_length=128,widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': "Username",'autofocus': ''}))
password = forms.CharField(label='密碼',max_length=256,widget=forms.PasswordInput(attrs={'class': 'form-control','placeholder': "Password"}))
captcha = CaptchaField(label="驗證碼") #添加這里
六、登錄目標login.html中代碼
{% load static %}
登錄{% if myform.captcha.errors %}
{{ myform.captcha.errors }}{% elif message %}
{{ message }}{% endif %}
{% csrf_token %}
歡迎登錄!
{{ myform.username.label_tag }}
{{ myform.username }}
{{ myform.password.label_tag }}
{{ myform.password }}
{{ myform.captcha.label_tag}}
{{ myform.captcha}}
新用戶注冊:
登錄
{# 以下三者的引用順序是固定的#}
其中圖形驗證碼關鍵代碼為:
{{ myform.captcha.label_tag}}
{{ myform.captcha}}
總結
以上是生活随笔為你收集整理的html的表单图形验证码怎么做,django中简单图形验证码实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 西安一公司招聘要求不能起诉公司:员工仅1
- 下一篇: CCIE-LAB-SDN-第六篇-SDW