django验证码框架(django-simple-captcha)

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输入框

forms.py
# 验证码
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': '验证码错误啊'})
view.py
# 密码 加密
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})
若报403错误:
</form>前添加:
{% csrf_token %}

猜你喜欢

转载自blog.csdn.net/weixin_37887248/article/details/80727837