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

Django学习系列之captcha 验证码插件

时间:2017-09-21 21:13:09      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:div   npv   效果   gen   style   账户   value   tty   reg   

安装部署

安装captcha

pip3.6 install  django-simple-captcha==0.4.6

settings.py中引入captcha

INSTALLED_APPS = [
    django.contrib.admin,
    django.contrib.auth,
    django.contrib.contenttypes,
    django.contrib.sessions,
    django.contrib.messages,
    django.contrib.staticfiles,
    captcha,
]

url.py配置

from django.conf.urls import url, include
from django.contrib import admin
 
urlpatterns = [
    url(r^admin/, admin.site.urls),
    url(r^captcha/, include(captcha.urls)),
]

数据库同步

makemigrations
migrate

应用

效果

技术分享

定义forms.py

from django import forms


from captcha.fields import CaptchaField

#对表单进行判断
class RegisterForm(forms.Form):
    email = forms.EmailField(required=True)
    password = forms.CharField(required=True,min_length=5)
    captcha = CaptchaField(error_messages={"invalid":"验证码错误"}) #加入这条

views.py

from django.views.generic.base import View
from .forms import RegisterForm

from django.shortcuts import render,HttpResponse

‘‘‘
注册账户‘‘‘ class RegisterView(View): #显示注册页面 def get(self,request): #把form传给前端,里边是验证码需要在前端显示 register_form = RegisterForm() return render(request,‘register.html‘,{‘register_form‘:register_form}) #接收提交的数据,进行逻辑判断 def post(self,request): register_form = RegisterForm(request.POST) if register_form.is_valid(): user_name = request.POST.get(‘email‘, ‘‘) #去库里查询有没有这个邮箱,有就是已经注册过了 if UserProfile.objects.filter(email=user_name): return render(request,‘register.html‘,{‘register_form‘:register_form,‘msg‘:‘用户已经存在‘}) #没有就是新用户,继续流程 else: #获取前端填的表单,写进user库里 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() send_register_email(user_name,‘register‘) return render(request,‘login.html‘) else: return render(request,‘register.html‘,{‘register_form‘:register_form})#把register_form传给前端是因为 这里包含错误信息,需要在前端显示

HTML页面引用验证码

<div class="tab-form">
                    <form id="email_register_form" method="post" action="{% url ‘register‘ %}" autocomplete="off">
                        <input type=hidden name=csrfmiddlewaretoken value=gTZljXgnpvxn0fKZ1XkWrM1PrCGSjiCZ />
                        <div class="form-group marb20 {% if register_form.errors.email %} errorput {% endif %}">
                            <label>邮&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;箱</label>
                            <input  type="text" id="id_email" name="email" value="None" placeholder="请输入您的邮箱地址" />
                        </div>
                        <div class="form-group marb8 {% if register_form.errors.password %} errorput {% endif %}">
                            <label>密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码</label>
                            <input type="password" id="id_password" name="password"  value="None" placeholder="请输入6-20位非中文字符密码" />
                        </div>
                        <div class="form-group marb8 captcha1 {% if register_form.errors.captcha %} errorput {% endif %} ">
                            <label>验&nbsp;证&nbsp;码</label>
                            {{ register_form.captcha }}  #加入这条
                        </div>
                        <div class="error btns" id="jsEmailTips">
                            {% for key, error in register_form.errors.items %}
                                {{ error }}
                            {% endfor %}
                            {{ msg }}
                        </div>
                        <div class="auto-box marb8">
                        </div>
                        <input class="btn btn-green" id="jsEmailRegBtn" type="submit" value="注册并登录" />
                        {% csrf_token %}
                    </form>
                </div>

流程:

1.在这个页面,填完邮箱,密码,验证码后提交表单

2.把表单数据交给django-form验证

3.forms.py的RegisterForm中email和password字段验证输入的数据符不符合forms中定义的格式要求(密码是不是小于5位之类的),captcha验证验证码输入是否正确,注意是不一样的

4.验证码的输入框和图片是captcha自动生成的 

Django学习系列之captcha 验证码插件

标签:div   npv   效果   gen   style   账户   value   tty   reg   

原文地址:http://www.cnblogs.com/zhaijunming5/p/7570180.html

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