安装:
pip install django-simple-captcha
将 captcha 添加到 settings.py 中的 INSTALLED_APPS
INSTALLED_APPS = [ 'captcha', ]
在 url.py 中添加一条路由
urlpatterns = [ path('captcha/',include('captcha.urls')), ]
生成数据库
python manage.py makemigrations python manage.py migrate
显示验证码
定义一个form表单
from captcha.fields import CaptchaField class RegisterForm(forms.Form): email = forms.EmailField(required=True) password = forms.CharField(required=True,min_length=5) # 验证码,字段里面可以自定义错误提示信息 captcha = CaptchaField()
通过{{ register_form.captcha }}获取验证码
<div class="form-group marb8 captcha1 "> <label>验 证 码</label> {{ register_form.captcha }} </div>