http://django-simple-captcha.readthedocs.io/en/latest/
pip install django-simple-captcha
略
from django.conf.urls import url,include
url(r'^captcha/', include('captcha.urls')), #添加到url
makemigrations
migrate
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'验证码错误'})
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()