django注册登陆验证码小结

注册登陆验证码 参考的是 :

    http://django-simple-captcha.readthedocs.io/en/latest/usage.html#adding-to-a-form

以下是前几部的操作 :

Using django-simple-captcha

Installation

  1. Install django-simple-captcha via pippip install django-simple-captcha

  2. Add captcha to the INSTALLED_APPS in your settings.py

  3. Run python manage.py migrate

  4. Add an entry to your urls.py:

    urlpatterns += [
        url(r'^captcha/', include('captcha.urls')),
    ]
    

Note: PIL and Pillow require that image libraries are installed on your system. On e.g. Debian or Ubuntu, you'd need these packages to compile and install Pillow:

apt-get -y install libz-dev libjpeg-dev libfreetype6-dev python-dev

定义要给form表单 :

#注册界面,captcha 验证码
class Register_Form(forms.Form):
    email = forms.EmailField(required=True)
    password = forms.CharField(max_length=10,min_length=3)
    captcha = CaptchaField(error_messages={"invalid":u"验证码错误"})

html前端页面:

        <form class="form-signin" role="form" method="POST" action="{% url 'registViews' %}">
            {% csrf_token %}
            <div>
                邮  箱:<input type="email" name="email" id="username"/>
            </div>
            <br>
            <div>
                密   码:<input type="password" name="password" id="pwd"/>
            </div>

            <div >
{#                <input type="text" name="captcha" id="captcha"/>#}
                验证码: {{ register_form.captcha }}
            </div>
            <div>
                <input type="submit" value="注册并登录"/>
                <input type="reset" name="reset" value="重置"/>
                <div id="result"></div>
            </div>

        </form>


views.py

#注册
def registViews(request):
    if request.method =="GET":
        register_form = Register_Form()
        return render(request,'rc_test/register.html',context= {'register_form':register_form })
    else:
        reg = Register_Form(request.POST)
        print reg
        if reg.is_valid():
            email = reg.cleaned_data['email']
            password = reg.cleaned_data['password']
            print email
            print password

            if UserInfo.objects.filter(email=email):
                return render(request,'rc_test/register.html',{'register_form':reg,
                                                               'msg':'用户已经存在'
                                                               })

            #如果没有注册 ,保存到数据库中 ,但是还没有被激活 ,发送邮件等待验证
            user_proinfo = UserInfo()
            user_proinfo.username = email
            user_proinfo.email = email
            user_proinfo.is_active = False
            user_proinfo.password = make_password(password)          #make_password() 是hash 的加密方式
            user_proinfo.save()
            #发送邮箱
            # send_register_email(email,'register')

            return HttpResponseRedirect(reverse('loginViews'))

        # return render(request,'rc_test/register.html')
        return HttpResponseRedirect(reverse('registViews'))

页面展示 :

定义一个userinfo的表存放注册信息

#用户信息表
class UserInfo(models.Model):
    username = models.CharField(max_length=50)
    password = models.CharField(max_length=100)
    email = models.EmailField()



猜你喜欢

转载自blog.csdn.net/hejunw/article/details/80158473