- https://django-simple-captcha.readthedocs.io/en/latest/usage.html
- pip install django-simple-captcha==0.4.6这是使用于django1.9的
- 在settings.py中加入
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users',
'courses',
'organization',
'operation',
'xadmin',
'crispy_forms',
'captcha',
]
- 在forms.py中加入
from captcha.fields import CaptchaField
class RegisterForm(forms.Form):
email = forms.EmailField(required=True)
password = forms.CharField(required=True, min_length=5)
captcha = CaptchaField(error_messages={"invalid": "验证码错误"})
#这里可以自定以错误信息
- 配置url
from django.conf.urls import url, include
+url(r'captcha/', include('captcha.urls')),
- 在views.py中添加
#下面的函数可以对密码进行加密
from django.contrib.auth.hashers import make_password
from .forms import LoginForm, RegisterForm
#在视图中创建
class RegisterView(View):
def get(self, request):
register_form = RegisterForm()
return render(request, 'register.html', {'register_form': register_form})
html代码
<div class="form-group marb8 captcha1 ">
<label>验 证 码</label>
{{ register_form.captcha }}//这里不用定义input,它会自动的生成input和验证码
</div>