码迷,mamicode.com
首页 > 其他好文 > 详细

第三方验证码插件配合form的使用 | Django

时间:2017-12-09 15:52:10      阅读:461      评论:0      收藏:0      [点我收藏+]

标签:字段   install   spel   表单   mes   sel   val   views   include   

1.第三方验证码插件
# 安装
    pip install django-simple-captcha
# settings.py
    captcha, # 生成图片路径地址的表
# 迁移
    makemigrations
    migrate
# 加入图片路径urls.py
    url(^chptcha/, include(captcha.urls)),
# 在form中使用图片验证码|edu_forms.py
    from captcha.fields import CaptchaField
    from django import form
    
    class Register(forms.Form):
        captcha = CaptchaField(error_messages={"invalid":"验证码异常!"})
        email = forms.ChaField(required=True)
        password = forms.ChaField(required=True, max_length=32)
# views.py
    from users.edu_forms import RegisterForm
    from django.views.generic.base import View
    
    class RegisterView(View):
        def get(request):
            # 实例化form表单
            register_form = RegisterForm()
            return render(request, "register.html", {
                register_form:register_form
            })
        def post(self, request):
            from django.http import HttpResponse
            from django.shortcuts imoprt redirect
            register_form = RegisterForm(request.POST)
            if register_form.is_valid()
                # 如果字段验证通过,注册逻辑
                # 取出表单数据
                user_name = request.POST.get("username", "")
                pass_word = request.POST.get("password", "")
                # 实例化用户模型类
                user_profile = UserProfile()
                user_profile.userrname=user_name
                user_profile.email = user_name
                # 明文密码加密
                from django.contrib.auth.hashers imoprt make_password
                user_userprofile.password = make_password(pass_word)
                user_profile.save()
                return redirect(reverse(login))
            else:
                return render(request, register.html, {
                    register_form:register_form
                })
# register.html
    {{ register_form.captcha }}
    # 这个字段在前端会生成什么?
    <img src="/chapcha/image/85de4f2435b124c65de34454ea6c380d79c1cd25/" alt="captcha" class="captcha" />
    <input id="id_captcha_0" name="captcha_0" type="hidden" value="85de4f2435b124c65de34454ea6c380d79c1cd25" />
    <input autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" id="id_captcha_1" name="captcha_1" type="text" />

 

第三方验证码插件配合form的使用 | Django

标签:字段   install   spel   表单   mes   sel   val   views   include   

原文地址:http://www.cnblogs.com/pymkl/p/8011486.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!