djangoform form using codes

8.1. Installation captcha

Direct installation: pip install django-simple-captcha

Django automatically help us install the relevant dependent libraries six, olefileand Pillowwhere the Pillow is the famous drawing module.

Registration captcha

In the settings, the 'captcha' registered with the app list:

 

INSTALLED_APPS = [

 

 

'django.contrib.admin',

'django.contrib.auth',

'django.contrib.contenttypes',

'django.contrib.sessions',

'django.contrib.messages',

'django.contrib.staticfiles',

'login',

'captcha',

]

 

captcha need to build their own data table in the database, you need to execute a command to generate migrate data sheet:

python manage.py migrate

8.2. Adding url routing

urls.py file in the root directory of a corresponding increase captcha URL:

  1.  
    from django.conf.urls import url
  2.  
    from django.conf.urls import include
  3.  
    from django.contrib import admin
  4.  
    from login import views
  5.  
     
  6.  
    urlpatterns = [
  7.  
    url(r'^admin/', admin.site.urls),
  8.  
    url(r'^index/', views.index),
  9.  
    url(r'^login/', views.login),
  10.  
    url(r'^register/', views.register),
  11.  
    url(r'^logout/', views.logout),
  12.  
    URL (R & lt '^ captcha' , the include ( 'captcha.urls' ) ) increases this line #
  13.  
    ]

8.3. Modify forms.py

If the above are OK, you can add CaptchaField directly in our forms.py the file.

  1. from django import forms
  2. from captcha.fields import CaptchaField
  3.  
  4. class UserForm(forms.Form):
    1.   username = forms.CharField(label="用户名", max_length=128, widget=forms.TextInput(attrs={'class': 'form-control'}))
    2.   password = forms.CharField(label="密码", max_length=256, widget=forms.PasswordInput(attrs={'class': 'form-control'}))
    3.   captcha = CaptchaField (label = 'PIN' )

You need to import in advance from captcha.fields import CaptchaField, then just like writing a normal form field to add a captcha field as on it!

 8.4. Modify login.html

 Since our previous manually-generated form form, therefore it has changed a bit, add relevant content captcha, as follows:

  1. <form class='form-login' action="/login/" method="post">
  2. < H2 class = "text-Center" > Welcome < / h2 >
  3. <div class="form-group">
  4. {{ login_form.username.label_tag }}
  5. {{ login_form.username}}
  6. </div>
  7. <div class="form-group">
  8. {{ login_form.password.label_tag }}
  9. {{ login_form.password }}
  10. </div>
  11. <div class="form-group"> 
  12. {{ login_form.captcha.errors }}
  13. {{ login_form.captcha.label_tag }}
  14. {{ login_form.captcha }} 
  15. </div>
  16. <button type="reset" class="btn btn-default pull-left">重置</button> 
  17. <button type="submit" class="btn btn-primary pull-right">提交</button>
  18. </form>
  19. </div> 
  20. </div> <!-- /container --> 
  21. {% endblock %}

Here a extra {{ login_form.captcha.errors }}for a clear indication of the user, your verification code is incorrect

 

 

For verification code refresh 

Modify login.html:

<div class="form-group">
{{login_form.captcha.label_tag}}<a id="refesh">刷新</a>
<p>
{{login_form.captcha}}
{{login_form.captcha.errors}}</p>

</div>

JS:
  // refresh achieving dynamic codes 
$ ( '# refesh'). The Click (function () {
$ .getJSON ( "/ captcha / Refresh /", function (Result) {
$ ( '. Captcha'). Attr ( 'the src ', Result [' image_url ']);
$ (' # id_captcha_0 ') Val (Result. [' Key '])
});
});

// return operation after the rear end of the verification fails
if (' {{status} } '==' error ') {
Alert ( "authentication failed login again!");
window.location.assign ( "/ Accounts / Login /")
}



Guess you like

Origin www.cnblogs.com/cou1d/p/12079164.html