当前位置:   article > 正文

Django验证码库之django-simple-captcha

django-simple-captcha

Django实现带验证码登录功能

_______________________________________________________________________

第一步安装:

  1. pip install django-simple-captcha
  2. sudo apt-get -y install libz-dev libjpeg-dev libfreetype6-dev python-dev
  3. pip install PIL
  4. pip install Pillow

第二步加载app:

  1. INSTALLED_APPS = (
  2. 'django.contrib.admin',
  3. 'django.contrib.auth',
  4. 'django.contrib.contenttypes',
  5. 'django.contrib.sessions',
  6. 'django.contrib.messages',
  7. 'django.contrib.staticfiles',
  8. 'authpermission',
  9. 'DjangoUeditor',
  10. 'app02',
  11. 'captcha', # 验证码
  12. )
'
运行

第三步同步数据库:

python manage.py migrate

第四步添加项目主url:

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

第五步定义表单增加验证码字段:

  1. from django import forms
  2. from captcha.fields import CaptchaField
  3. class RegisterForm(forms.Form):
  4. """
  5. 注册表单
  6. """
  7. username = forms.EmailField(
  8. widget=forms.TextInput(
  9. attrs={
  10. "class": "form-control",
  11. "placeholder": "请输入邮箱账号",
  12. "value": "",
  13. "required": "required"
  14. }
  15. ),
  16. max_length=50,
  17. error_messages={"required": "用户名不能为空", }
  18. )
  19. password = forms.CharField(
  20. widget=forms.PasswordInput(
  21. attrs={
  22. "class": "form-control",
  23. "placeholder": "请输入密码",
  24. "value": "",
  25. "required": "required"
  26. }
  27. ),
  28. min_length=8,
  29. max_length=50,
  30. error_messages={"required": "密码不能为空", }
  31. )
  32. # 验证码
  33. captcha = CaptchaField()

第六步在视图函数(views)中验证表单:

  1. def some_view(request):
  2. if request.POST:
  3. form = CaptchaTestForm(request.POST)
  4. # Validate the form: the captcha field will automatically
  5. # check the input
  6. if form.is_valid():
  7. human = True
  8. else:
  9. registerform = RegisterForm()
  10. return render(
  11. request, "register.html",
  12. {"registerform": registerform})
'
运行

第七步骤html页面展示:

  1. {% load static %}
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>Register</title>
  7. <script src="{% static 'js/jquery-1.12.4.min.js' %}"></script>
  8. <script src="{% static 'js/register.js' %}"></script>
  9. </head>
  10. <body>
  11. <div class="cls-content">
  12. <div class="cls-content-sm panel">
  13. <p class="pad-btm">Sign In to your account</p>
  14. {{ registerform }}
  15. <br><br>
  16. <input type="submit" name="submit" id="" value="注册">
  17. </div>
  18. </div>
  19. </body>
  20. </html>

第八步使用js动态刷新验证码:

  1. $(document).ready(function () {
  2. $(".captcha").click(function () {
  3. $.ajax({
  4. url: '/captcha/refresh/',
  5. type: 'GET',
  6. success: function (result) {
  7. $(".captcha").attr('src', result["image_url"]);
  8. $("#id_captcha_0").val(result["key"])
  9. }
  10. })
  11. });
  12. });

附加:一个挺详细的django-simple-captcha文档

https://django-simple-captcha.readthedocs.io/en/latest/usage.html

 

注意:以上内容是个人使用的随手记录, 就是介绍了下简单的使用

欢迎大家来吐槽,准备好瓜子饮料矿泉水,开整!!!

---------------------------------------------------------------------------------------

搞笑一则:能动手尽量别吵吵

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/983034
推荐阅读
相关标签
  

闽ICP备14008679号