参考文档
http://django-simple-captcha.readthedocs.io/en/latest/
django支持1.7+
1.安装
pip install django-simple-captcha
2.添加到install_apps中
略
3.添加以下url到urls文件中
from django.conf.urls import url,include
url(r'^captcha/', include('captcha.urls')), #添加到url
4.生成数据库
makemigrations
migrate
5.应用配置
- 在forms.py中导入以下模块
from captcha.fields import CaptchaField
在下关Forms中使用
class RegisterForm(forms.Form):
email = forms.EmailField(required=True)
password = forms.CharField(required=True,min_length=5)
captcha = CaptchaField(error_messages={'invalid':u'验证码错误'})
- 在views.py中使用forms
class RegisterView(View):
def get(self,request):
register_form = RegisterForm()
return render(request,'register.html',{'register_form':register_form})
def post(self,request):
register_form = RegisterForm()
if register_form.is_valid():
user_name = request.POST.get("username", '')
pass_word = request.POST.get("password", '')
user_profile = UserProfile()
user_profile.username = user_name
user_profile.email = user_name
user_profile.password = make_password(pass_word)
user_profile.save()