Verification Code function

Source: Internet
Author: User
Tags install django pip install django

CAPTCHA function 1. Installing the CAPTCHA Plugin
(Dj_login) D:\dj\dj_login>pip Install django-simple-captchacollecting django-simple-captcha Using cached https:// files.pythonhosted.org/packages/d7/f4/ea95b04ed3abc7bf225716f17e35c5a185f6100db4d7541a46696ce40351/ Django-simple-captcha-0.5.9.ziprequirement already satisfied:six>=1.2.0 in E:\venv\dj_login\lib\site-packages ( From Django-simple-captcha) (1.11.0) requirement already satisfied:django>=1.8 in e:\venv\dj_login\lib\  Site-packages (from Django-simple-captcha) (1.11.12) collecting pillow!=5.1.0,>=2.2.2 (from Django-simple-captcha) Using Cached https://files.pythonhosted.org/packages/1b/50/ 869910cd7110157fbefd0fed3db3656c1951f1bceecdd00e3716aa269609/pillow-5.2.0-cp36-cp36m-win_amd64.whlcollecting django-ranged-response==0.2.0 (from Django-simple-captcha) Using cached https://files.pythonhosted.org/packages/70/ E3/9372fcdca8e9c3205e7979528ccd1a14354a9a24d38efff11c1846ff8bf1/django-ranged-response-0.2.0.tar.gzrequirement Already Satisfied:pytz in E:\venv\dj_login\lib\sitE-packages (from Django>=1.8->django-simple-captcha) (2018.5) installing collected Packages:pillow, Django-ranged-response, Django-simple-captcha Running setup.py install for django-ranged-response ... done Running setup . py Install for Django-simple-captcha ... donesuccessfully installed Pillow-5.2.0 django-ranged-response-0.2.0 django-simple-captcha-0.5.9
2. Register CAPTCHA and create a table
INSTALLED_APPS = [    'django.contrib.admin',    'django.contrib.auth',    'django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'django.contrib.staticfiles',    'app_login',    'xadmin',    'crispy_forms',    'captcha'](dj_login) D:\dj\dj_login>python manage.py migrateOperations to perform:  Apply all migrations: admin, app_login, auth, captcha, contenttypes, sessions, xadminRunning migrations:  Applying captcha.0001_initial... OK
3. Add a route
from django.conf.urls import urlfrom django.conf.urls import includeimport xadminurlpatterns = [    # path('admin/', admin.site.urls),    url('xadmin/',xadmin.site.urls),    url(r'',include('app_login.urls')),    url(r'^captcha',include('captcha.urls'))]
4. Modify the forms.py form
from django import forms   #导入forms模块from captcha.fields import CaptchaField   #导入验证码功能模块class UserForm(forms.Form):   #所有表单类都需要继承forms.Form    '''定义form子标签<input>子元素的name属性变量,这里定义的是name=username和name=password的变量label就是label标签,max_length为设置该变量可以输入的最大字符widget为设置input的类型,这里的passwordinput为type='password',添加属性使用attrs={}以字典键值对的形式,这里增加了class和placeholder属性'''    username=forms.CharField(label='用户名',max_length=128,widget=forms.TextInput(attrs={'class':'form-control','placeholder':'用户名'}))    password=forms.CharField(label='密码',max_length=512,widget=forms.PasswordInput(attrs={'class':'form-control','placeholder':'密码'}))    captcha=CaptchaField(label='验证码')   #添加验证码表单字段
5. Modify the login.html file
{% extends ' base.html '%} <!--inherit base page template content, can browse Web page view effect-->{% load staticfiles%} <!--load static method for loading files in static--&gt ; {% block title%} login {% Endblock%} <!--set title, default using-->{% block CSS%}<link href= "{% static" Css/login.cs in base template S '%} "rel=" stylesheet "/>{% endblock%} <!--use a custom CSS landing style without-->{% block content% in base} <!--the template language is used to define the body content, Combined with templates--<div class= "container" > <div class= "col-md-4 col-md-offset-4" > <form CLA                  ss= ' Form-login ' action= "/login/" method= "POST" > {% if message%} <!--like if statement-- <div class= "alert alert-warning" >{{message}}</div> <!--use bootstrap alert to pass message message--{% endif%} {% Csrf_token%} 

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.