-
配置
$ pip install django-simple-captcha # 如果安装有依赖库问题,请执⾏下⾯的安装 apt-get -y install libz-dev libjpeg-dev libfreetype6-dev python-dev # settings.py INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'App02', 'App', 'captcha' # 安装应用 ] # 图形验证码配置 # 证码设置 CAPTCHA_IMAGESIZE = (8,45) # 设置captcha图片大小 CAPTCHA_LENGTH =4 #字符个数 CAPTCHA_TIMEOUT =1 #超时(minutes)* # 输出格式:输入框验证码图片隐藏域• # '%(image)s %(hidden_field)s %(text_field)s' CAPTCHA_OUTPUT_FORMAT ='%(text_field)s %(image)s %(hidden_field)s' CAPTCHA_NOISE_FUNCTIONS =( 'captcha.helpers.noise_null', 'captcha.helpers.noise_arcs', 'captcha.helpers.noise_dots', ) CAPTCHA_CHALLENGE_FUNCT = 'captcha.helpers.random_char_challenge' # 最后要迁移数据库: # python manage.py migrate
-
实现
from django import forms from captcha.fields import CaptchaField class LoginForm(forms.Form): captcha = CaptchaField() # 验证码字段
def handle_captcha(request): if request.method == "POST": form = LoginForm(request.POST,request) print(form) if form.is_valid(): print("验证通过") return HttpResponse("验证通过") else: return render(request, 'app/verifycode.html', locals()) else: form = LoginForm() return render(request,'app/verifycode.html',locals())
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="{% url 'App:yzm' %}" method="post"> {% csrf_token %} <input type="text" name="yzm"> <img src="{{ image_url }}" alt=""> <br> <input type="hidden" name="code" value="{{ new_key }}"> <input type="submit"> </form> </body> </html>
Django—图形验证码
最新推荐文章于 2024-03-15 23:08:41 发布