django-simple-captcha 驗證碼插件


官方文檔:http://django-simple-captcha.readthedocs.io/en/latest/usage.html#installation

github:https://github.com/mbi/django-simple-captcha

 

安裝部署

版本:django 1.9 

pip install  django-simple-captcha==0.4.6
  • settings.py配置,加入captcha
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'captcha',
]
  • urls.py配置

加入url(r'^captcha/', include('captcha.urls')),

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^captcha/', include('captcha.urls')),
]
  • 數據庫同步
makemigrations
migrate

 

應用場景

  • form定義

app下自定義froms.py文件,創建一個注冊form

from django import forms
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": u"驗證碼錯誤"})
  • views.py代碼
from django.views.generic.base import View

class RegisterView(View):

    def get(self, request):
        register_form = RegisterForm()
        return render(request, "register.html", {"register_form": register_form})
  • html頁面引用
{{ register_form.captcha }}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM