利用django-simple-captcha生成验证码

2018/03/06 16:42
阅读数 46

参考文档

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.应用配置

  1. 在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'验证码错误'})
  1. 在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()

6.前端渲染即可

image

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部