赞
踩
django-simple-captcha
通过pip安装:pip install django-simple-captcha
添加captcha
到INSTALLED_APPS
您的settings.py
跑 python manage.py migrate
将条目添加到您的urls.py
:
- urlpatterns += [
- path('captcha/', include('captcha.urls')),
- ]
注意:Pillow需要在系统上安装映像库。在例如Debian或Ubuntu上,您需要以下软件包才能编译和安装Pillow:
apt-get -y install libz-dev libjpeg-dev libfreetype6-dev python-dev
(1) 在应用users下面的forms.py文件中封装图片验证码类:
- from django import forms
- from captcha.fields import CaptchaField
-
-
- class DynamicLoginForm(forms.Form):
- captcha = CaptchaField()
(2)登录视图views.py中将图片验证码实例返回
- from django.views.generic.base import View
- from apps.users.forms import LoginForm, DynamicLoginForm
-
-
- class LoginView(View):
- """登录"""
- def get(self, request):
- if request.user.is_authenticated:
- return redirect(reverse("courses:index"))
-
- login_form = DynamicLoginForm()
- return render(request, "login.html", {"login_form": login_form})
(3)登录页面中直接使用动态登录验证码实例渲染成HTML
- <div class="form-group marb20 blur" id="jsRefreshCode">
- {{ login_form.captcha }}
- </div>
点击验证码就会刷新!
详细内容请查看django-simple-captcha官网:
https://django-simple-captcha.readthedocs.io/en/latest/usage.html#adding-to-a-form
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。