1,安裝第三方庫html
pip install django-simple-captcha
2,註冊應用python
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'login', 'captcha', ]
3,添加路由django
urlpatterns = [ path('admin/', admin.site.urls), path('index/',views.index), path('login/',views.login), path('register/',views.register), path('logout/',views.logout), path('captcha/', include('captcha.urls')), ]
4,修改form模型session
class UserForm(forms.Form): username = forms.CharField(label="用戶名", max_length=128, widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': "Username",'autofocus': ''})) password = forms.CharField(label="密碼", max_length=256, widget=forms.PasswordInput(attrs={'class': 'form-control','placeholder': "Password"})) captcha = CaptchaField(label='驗證碼')
5,執行數據遷移url
python manage.py migrate
6,修改login.htmlcode
添加orm
<div class="form-group"> {{ login_form.captcha.label_tag }} {{ login_form.captcha }} </div>