1.安装django-simple-captcha
pip install django-simple-captcha
# 如果安装有依赖库问题,请执⾏下⾯的安装
# apt-get -y install libz-dev libjpeg-dev libfreetype6-dev python-dev
2.配置应用/验证码样式(settings.py)
# 注册应用
INSTALLED_APPS = [
'captcha'
]
# captcha验证码样式设置
# 设置字体大小
CAPTCHA_FONT_SIZE = 20
# 生成验证码的图像大小(以像素为单位),由2元组(宽度,高度)指定
CAPTCHA_IMAGE_SIZE = (87,37)
# 验证码的背景颜色
CAPTCHA_BACKGROUND_COLOR = "#ccff99"
# 验证码的字体颜色
CAPTCHA_FOREGROUND_COLOR = "#330066"
# 生成的验证码的寿命(以分钟为单位)
CAPTCHA_TIMEOUT = 1
# 生成的验证码的长度(以字符为单位)
CAPTCHA_LENGTH = 4
# 随机字符验证码
CAPTCHA_CHALLENGE_FUNCT = 'captcha.helpers.random_char_challenge'
# # 数学验证码
# CAPTCHA_CHALLENGE_FUNCT = 'captcha.helpers.math_challenge'
# 字典词字符验证码
# CAPTCHA_CHALLENGE_FUNCT = 'captcha.helpers.word_challenge'
# 输出格式:输入框验证码图片隐藏域
CAPTCHA_OUTPUT_FORMAT ='%