暮梅在线教育平台-注册功能

  • A+
所属分类:Django框架

图片验证码(django-simple-captcha模块)

将'captcha'加入到setting.py文件下INSTALLED_APPS中

将路径加入到urls.py文件

from django.urls import path,include,re_path

path('captcha/',include('captcha.urls')), # include功能,将此页面的path解析交给它自己的urls.py文件

运行makemigrations,migrate

在forms.py文件中定义类

from captcha.fields import CaptchaField

class RegisterForm(forms.Form):

    email = forms.EmailField(required=True)

    password = forms.CharField(required=True, min_length=5)

    captcha = CaptchaField(error_messages={'invalid':'验证码错误'}) # 定制错误信息

在views.py文件中定义类

from django.contrib.auth.hashers import make_password

class RegisterView(View):

    def get(self,request):

       register_form = RegisterForm()

       return render(request, 'register.html', {'register_form':register_form})

    def post(self,request):

       register_form = RegisterForm(request.POST)

       if register_form.is_valid():

          user_name = request.POST.get('email','')

          if UserProfile.objects.filter(email=user_name):

             return render(request, 'register.html', {'register_form': register_form,"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() #保存到数据库

          send_register_email(user_name,'register')

          return render(request, 'login.html')

else:

          return render(request, 'register.html', {'register_form': register_form})

在前端界面添加验证码代码

<div class="form-group marb8 captcha1 {% if login_form.errors.captcha %}errorput{% endif %}">

    <label>验&nbsp;证&nbsp;码</label>

    {{ register_form.captcha }}

</div>

  • 微信
  • 等你哟
  • weinxin
  • 微信公众号
  • mac软件资源等你来拿
  • weinxin
小羊同学

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: