Django Simple Captcha function
Dependency Packages
Libz-dev Libjpeg-dev Libfreetype6-dev Python-dev
One, download the package, copy the CAPTCHA to the project
Https://github.com/mbi/django-simple-captcha
Second, the operation
Pip Install Django-simple-captcha
Third, add to settings.py
Installed_apps = [' Captcha ',]
Four, the operation
Python manage.py Migrate
V. Modification of urls.py
From django.conf.urls import url,includeurlpatterns = [url (r ' ^captcha/', include (' Captcha.urls ')),]
Vi. Modification of form
form.py adding CAPTCHA
From captcha.fields import captchafieldclass loginform (forms. Form): username = forms. Charfield (label= ' username ', widget=forms. TextInput (attrs={"placeholder": "user name", "required": "required",}), max_length=50, error_messages={"Required": " Username cannot be empty ",}) password = forms. Charfield (label= ' password ', widget=forms. Passwordinput (attrs={"placeholder": "password", "required": "required",}), max_length=20, error_messages={"Required": " Password cannot be empty ",}) captcha = captchafield (label= ' Verification code') def clean (self): #验证码 try: captcha_x = self.cleaned_data[' Captcha '] except exception as e: print (' except: ' + str (e)) raise forms. ValidationError ("Wrong verification code, please re-enter")
This article is from the "Silver Fox" blog, please be sure to keep this source http://foxhound.blog.51cto.com/1167932/1828510
Django Verification Code