django1.9关于部署在注册时的验证码

  1. https://django-simple-captcha.readthedocs.io/en/latest/usage.html
  2. pip install django-simple-captcha==0.4.6这是使用于django1.9的
  3. 在settings.py中加入
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'users',
    'courses',
    'organization',
    'operation',
    'xadmin',
    'crispy_forms',
    'captcha',
]
  1. 在forms.py中加入

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": "验证码错误"})
    #这里可以自定以错误信息
  1. 配置url
from django.conf.urls import url, include
+url(r'captcha/', include('captcha.urls')),
  1. 在views.py中添加
 #下面的函数可以对密码进行加密
from django.contrib.auth.hashers import make_password
 from .forms import LoginForm, RegisterForm
 #在视图中创建
class RegisterView(View):
    def get(self, request):
        register_form = RegisterForm()
        return render(request, 'register.html', {'register_form': register_form})

html代码

<div class="form-group marb8 captcha1 ">
    <label>&nbsp;&nbsp;</label>
    {{ register_form.captcha }}//这里不用定义input,它会自动的生成input和验证码
</div>

猜你喜欢

转载自blog.csdn.net/qq_41682681/article/details/82748884