Django中验证码captcha的使用

本文介绍了如何在Django项目中集成并使用captcha验证码,包括安装django-simple-captcha库,配置settings.py,设置urls,数据库迁移,form类中添加captcha字段,后台逻辑处理以及在前端展示验证码的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 安装命令

pip install django-simple-captcha

2. 讲captcha 在settings.py 文件中配置

INSTALLED_APPS中

INSTALLED_APPS = [
	'django.contrib.admin',
	'django.contrib.auth',
	'django.contrib.contenttypes',
	'django.contrib.sessions',
	'django.contrib.messages',
	'django.contrib.staticfiles',
	'users.apps.UsersConfig',
	'orgs.apps.OrgsConfig',
	'operations.apps.OperationsConfig',
	'courses.apps.CoursesConfig',
	'xadmin',
	'crispy_forms',
	'captcha'
]

3.将captcha配置url

urlpatterns = [
	# url(r'^admin/', admin.site.urls),
	url(r'^xadmin/', xadmin.site.urls),
	url(r'^media/(?P<path>.*)$', serve, {"document_root": MEDIA_ROOT}),
	url(r'^captcha/', include('captcha.urls')),
	url(r'^$', index, name='index'),
	url(r'^users/', include('users.urls', namespace='users')),
	url(r'^orgs/', include('orgs.urls', namespace='orgs')),
	url(r'^courses/', include('courses.urls', namespace='courses')),
	url(r'^operations/', include('operations.urls', namespace='operations')),

]

4.迁移同步,生成captcha所依赖的表

迁移:python manage.py makemigrations

同步:python manage.py migrate

5.将captcha字段在form类中进行设置

class UserRegisterForm(forms.Form):
	email = forms.EmailField(required=True, error_messages={
		'required': '邮箱不能为空'
	})
	password = forms.CharField(max_length=20, min_length=3, required=True, error_messages={
		'max_length': '密码的最大长度是20位',
		'min_length': '密码的最小长度是3位',
		'required': '密码不能为空'
	})
	captcha = CaptchaField(error_messages={
		'invalid': '验证码错误'
	})

6.在后台逻辑中,GET请求里面实例化我们的form,将form对象返回到页面

def user_register(request):
	if request.method == 'GET':
		user_register_form = UserRegisterForm()
		return render(request, 'user/user_register.html', {
			'user_register_form': user_register_form
		})
	else:
		user_register_form = UserRegisterForm(request.POST)
		if user_register_form.is_valid():
			email = user_register_form.cleaned_data['email']
			password = user_register_form.cleaned_data['password']
			user = UserProfile.objects.filter(Q(email=email) | Q(username=email))
			if user:
				return render(request, 'user/user_register.html', {
					'msg': '用户已存在'
				})
			else:
				a = UserProfile()
				a.email = email
				a.username = email
				a.set_password(password)
				a.save()

				send_email_verify.delay(email, 'register')
				return HttpResponse('邮件已发送,请去激活')
		else:
			return render(request, 'user/user_register.html', {
				'user_register_form': user_register_form
			})

7.在页面上通过{{ form对象.captcha }}获取验证码

<div class="form-group marb8 captcha1 ">
     <label>验&nbsp;证&nbsp;码</label>
     {{ user_register_form.captcha }}
</div>
<div class="error btns" id="jsEmailTips">
     {{ msg }}
     {% for key,err in user_register_form.errors.item %}
         {{ err }}
     {% endfor %}
</div>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值