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
- Project address: http://code.google.com/p/django-simple-captcha/
- Description: it is a third-party django APP used to add Verification Code images to the form.
II. General Usage
- Download python setup. py install for Installation
- Decompress the downloaded package and copy the captcha package to the project directory, such as myblog/captcha.
- Add:? In settings. py :?
1 |
INSTALLED_APPS(‘captcha’,) |
- In urls. py, add :?
1 |
(r’^captcha / ’, include(‘captcha.urls’)), |
- Execute python manage. py syncdb to generate the required database table
- 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() |
- 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:
- Perform steps 1 to 5
- 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() |
- In the Python27 \ Lib \ site-packages \ django \ contrib \ comments \ views \ comments. py file, add :?
- 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/