您好,登錄后才能下訂單哦!
Django簡單的驗證碼功能
依賴包
libz-dev libjpeg-dev libfreetype6-dev python-dev
一、下載包,把captcha復制到項目中
https://github.com/mbi/django-simple-captcha
二、運行
pip install django-simple-captcha
三、添加到settings.py
INSTALLED_APPS = [ 'captcha', ]
四、運行
python manage.py migrate
五、修改urls.py
from django.conf.urls import url,include urlpatterns = [ url(r'^captcha/', include('captcha.urls')), ]
六、修改form
form.py 添加captcha
from captcha.fields import CaptchaField class LoginForm(forms.Form): username = forms.CharField(label='用戶名',widget=forms.TextInput(attrs={"placeholder": "用戶名", "required": "required",}), max_length=50, error_messages={"required": "username不能為空",}) password = forms.CharField(label='密碼',widget=forms.PasswordInput(attrs={"placeholder": "密碼", "required": "required",}), max_length=20, error_messages={"required": "password不能為空",}) captcha = CaptchaField(label='驗證碼') def clean(self): #驗證碼 try: captcha_x = self.cleaned_data['captcha'] except Exception as e: print ('except: ' + str(e)) raise forms.ValidationError(u"驗證碼有誤,請重新輸入")
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。