github详情:
http://django-simple-captcha.readthedocs.io/en/latest/usage.html
pip install django-simple-captcha==0.4.6
将captcha添加到setting的app中:
以下添加到urls中:
url(r'^captcha/', include('captcha.urls')),
register.html中,调入下面:
{{ register_from.captcha }}
自动生成验证码图片和input输入框
# 验证码from captcha.fields import CaptchaField
# form对注册表单的验证class RegisterForm(forms.Form): email = forms.EmailField(required=True) password = forms.CharField(required=True, min_length=5) # 验证码,参数:错误信息 captcha = CaptchaField(error_messages={'invalid': '验证码错误啊'})
# 密码加密 from django.contrib.auth.hashers import make_password
class RegisterView(View): def get(self, request): register_from = RegisterForm() return render(request, "register.html", {'register_from': register_from}) def post(self, request): register_from = RegisterForm(request.POST) if register_from.is_valid(): user_name = request.POST.get('username', '') pass_word = request.POST.get('password', '') user_profile = UserProfile() user_profile.username = user_name user_profile.password = pass_word # 对密码加密 user_profile.password = make_password(pass_word) user_profile.save() # 保存到数据库 pass return render(request, "register.html", {'register_from': register_from})
在</form>前添加:
{% csrf_token %}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。