Django learning notes-Verification Code

Source: Internet
Author: User

Most blog websites do not set the user logon function. If no verification code mechanism is added to the form, you will be waiting for spammer to launch a crazy attack. I once suffered a big loss: on average, a previous website had dozens of spam messages every day, so it was too late to delete them. Later, I found an excellent plug-in for Blocking spam-akismet on the Internet. After using the plug-in, the effect was good. Basically, all the junk information was blocked, and I couldn't see it on the front-end, however, the problem is that these spam comments are constantly written into your database tables and can still be seen in the management background. Therefore, in order to get bored, I decided to add a verification code mechanism to the website once and for all. Although it is complicated to submit a form later, the website was finally cleaned up.

What are the advantages of using python or django? When you want to implement a function, you can search for it online. Many times, you will find that someone has solved this problem and released the relevant libraries. You can use it directly. Search for "django, verification code", and django-simple-captcha appeared. After reading its introduction, we found that it is a very useful third-party django verification code library, then I used it on my blog website. Next we will record its usage.

I. Introduction

  1. Project address: http://code.google.com/p/django-simple-captcha/
  2. Description: it is a third-party django APP used to add Verification Code images to the form.

II. General Usage

  1. Download python setup. py install for Installation
  2. Decompress the downloaded package and copy the captcha package to the project directory, such as myblog/captcha.
  3. Add:? In settings. py :?
    1 INSTALLED_APPS(‘captcha’,)
  4. In urls. py, add :?
    1 (r’^captcha/’, include(‘captcha.urls’)),
  5. Execute python manage. py syncdb to generate the required database table
  6. Add the following code to the appropriate location in the forms. py file that requires captcha and add the verification code field :?
    123 from
    captcha.fields
    import
    CaptchaField
     captcha=CaptchaField()
  7. Add :?
    12 if
    form.is_valid():
        human
    = True

Iii. Methods used in combination with the comments component of django

If the comment system in your project uses the built-in django comments library directly, the comments Library and the verification code library are used as follows:

  1. Perform steps 1 to 5
  2. In the Python27 \ Lib \ site-packages \ django \ contrib \ comments \ forms. py file (Path in windows), add the verification code field in the CommentDetailsForm class :?
    123 from
    captcha.fields
    import
    CaptchaField
     captcha=
    CaptchaField()
  3. In the Python27 \ Lib \ site-packages \ django \ contrib \ comments \ views \ comments. py file, add :?
    1 human=True
  4. Modify a form template: If the comment form template uses {form} directly, you do not need to modify anything. If it is a custom form template, you can add the following :?
    123 <p><label
    for="id_captcha"> Verification (required): </label> {{form.captcha}}</p>

In this way, we can basically use it and take a screenshot. Let's take a look:

Source: http://newliu.com/post/5/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.