第三方验证码插件配合form的使用 | Django
Posted 胡说八道
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第三方验证码插件配合form的使用 | Django相关的知识,希望对你有一定的参考价值。
1.第三方验证码插件 # 安装 pip install django-simple-captcha # settings.py ‘captcha‘, # 生成图片路径地址的表 # 迁移 makemigrations migrate # 加入图片路径urls.py url(‘^chptcha/‘, include(‘captcha.urls‘)), # 在form中使用图片验证码|edu_forms.py from captcha.fields import CaptchaField from django import form class Register(forms.Form): captcha = CaptchaField(error_messages={"invalid":"验证码异常!"}) email = forms.ChaField(required=True) password = forms.ChaField(required=True, max_length=32) # views.py from users.edu_forms import RegisterForm from django.views.generic.base import View class RegisterView(View): def get(request): # 实例化form表单 register_form = RegisterForm() return render(request, "register.html", { ‘register_form‘:register_form }) def post(self, request): from django.http import HttpResponse from django.shortcuts imoprt redirect register_form = RegisterForm(request.POST) if register_form.is_valid() # 如果字段验证通过,注册逻辑 # 取出表单数据 user_name = request.POST.get("username", "") pass_word = request.POST.get("password", "") # 实例化用户模型类 user_profile = UserProfile() user_profile.userrname=user_name user_profile.email = user_name # 明文密码加密 from django.contrib.auth.hashers imoprt make_password user_userprofile.password = make_password(pass_word) user_profile.save() return redirect(reverse(‘login‘)) else: return render(request, ‘register.html‘, { ‘register_form‘:register_form }) # register.html {{ register_form.captcha }} # 这个字段在前端会生成什么? <img src="/chapcha/image/85de4f2435b124c65de34454ea6c380d79c1cd25/" alt="captcha" class="captcha" /> <input id="id_captcha_0" name="captcha_0" type="hidden" value="85de4f2435b124c65de34454ea6c380d79c1cd25" /> <input autocapitalize="off" autocomplete="off" autocorrect="off" spellcheck="false" id="id_captcha_1" name="captcha_1" type="text" />
以上是关于第三方验证码插件配合form的使用 | Django的主要内容,如果未能解决你的问题,请参考以下文章