django 安装验证码插件captcha

时间:2024-05-24 07:36:31

安装captcha 

安装链接 http://django-simple-captcha.readthedocs.io/en/latest/usage.html#installation

1 . pip install  django-simple-captcha==0.4.6 

2.在settings.py的原有app上添加'captcha'如图:

django 安装验证码插件captcha

3 .迁移数据库

(1)makemigrations 

(2)migrate

4.添加url,如图

urlpatterns += [
    url(r'^captcha/', include('captcha.urls')),
]

django 安装验证码插件captcha

5.forms.py设置表单验证

from django import forms
from captcha.fields import CaptchaField

class CaptchaTestForm(forms.Form):
    username = forms.CharField(required=True)
    password=forms.CharField(required=True,min_length=5)
    captcha = CaptchaField()
6.views.py
from .froms import RegisterForm

 from  .models import UserProfile

class RegisterView(View):      

  def get(self, request):
       register_form = ReForm()
       return render(request, "register.html", {'register_form': register_form})#register_form返回表单验证信息

  def post(self,request):
       regform = ReForm(request.POST)
         if register_from.is_valid():

                              user_name=request.POST.get("username","")

                               if UserProfile.object.filter(username=user_name) :

                                              return render(request,"register.html",{"msg":"此用户已存在"})

                              pass_word=request.POST.get("password","")

                              user_profile=UserProfile()

                              user_profile.username=user_name

                              user_profile.email=user_name

                              user_profile.is_active=False

                             user_profile.password=make_password(pass_word)

                             user_profile.save()

                             #发送邮件**注册账号

                             register_send_email=(username,"register")#调用9的方法发送邮件

                            

            

7在register.html中输出{{register_form}}这样验证码就会成功了

**********************************************************************************以下是发邮件的

8.发送邮件的配置settings.py 设置email,

(1)
EMAIL_HOST_USER="[email protected]"即要发送邮件的邮箱要开启smtp如图:


(2)settings.py设置
EMAIL_HOST="smtp.sina.com"
EMAIL_PORT=25
EMAIL_HOST_USER="[email protected]"
EMAIL_HOST_PASSWORD="mllXXXXXXX"
EMAIL_USE_TLS=False
EMAIL_FROM="[email protected]"

9utils文件夹下的send_email.py 用于发送邮件给注册用户

from random import Random
from users.models import  EmailVerifyRecord
from django.core.mail import  send_mail #发送邮件
from  MxOnline.settings import EMAIL_FROM#导入邮件发送人
#随机字符串
def random_str(randomlength=8):
    str = ''
    chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
    length = len(chars) - 1
    random = Random()
    for i in range(randomlength):
        str += chars[random.randint(0, length)]
    return str

   def  register_send_email(email,send_type="register"):

          email_record=EmailVerifyRecord()

          code=random_str(16)

          email_record.email=email

          email_record.code=code

          email_record.send_type=send_type

          email_title=""

          email_body=""

          if send_type=="register":

                        

        email_title = "在线网注册**链接"
         email_body = "请点击下面的链接**你的账号: http://127.0.0.1:8000/active/{0}".format(code)

         

           send_status = send_mail(email_title, email_body, EMAIL_FROM, [email])
             if send_status:
                   pass