Django搭建登录注册功能
极验验证码使用教程
- 依赖
pip install geetest
- 导入包
from geetest import GeetestLib
极验验证功能实战源码
验证源码
# 极验验证码
pc_geetest_id = "d25d66e1eba4a79b97cbe360f4932617"
pc_geetest_key = "5adc3a6f063632aff82ea78a16db6b03"
# 处理极验 获取验证码的视图
def get_geetest(request):
user_id = 'lxgzhw@163.com'
gt = GeetestLib(pc_geetest_id, pc_geetest_key)
status = gt.pre_process(user_id)
request.session[gt.GT_STATUS_SESSION_KEY] = status
request.session["user_id"] = user_id
response_str = gt.get_response_str()
return HttpResponse(response_str)
前端JavaScript源码
<!-- 引入封装了failback的接口--initGeetest -->
<script src="http://static.geetest.com/static/tools/gt.js"></script>
<script>
$(function () {
// 极验 发送登录数据的
var handlerPopup = function (captchaObj) {
// 成功的回调
captchaObj.onSuccess(function () {
var validate = captchaObj.getValidate();
// 1. 取到用户填写的用户名和密码 -> 取input框的值
var username = $("#id_username").val();
var password = $("#id_password").val();
$.ajax({
url: "/blog/login/", // 进行二次验证
type: "post",
dataType: "json",
data: {
username: username,
password: password,
csrfmiddlewaretoken: $("[name='csrfmiddlewaretoken']").val(),
geetest_challenge: validate.geetest_challenge,
geetest_validate: validate.geetest_validate,
geetest_seccode: validate.geetest_seccode
},
success: function (data) {
if (data.status) {
// 有错误,在页面上提示
$(".text-danger").text(data.msg);
} else {
// 登陆成功
location.href = data.msg;
}
}
});
});
// 将验证码加到id为captcha的元素里
captchaObj.appendTo("#popup-captcha");
// 更多接口参考:http://www.geetest.com/install/sections/idx-client-sdk.html
};
// 当input框获取焦点时将之前的错误清空
$("#id_username,#id_password").focus(function () {
// 将之前的错误清空
$(".text-danger").text("");
});
// 验证开始需要向网站主后台获取id,challenge,success(是否启用failback)
$.ajax({
url: "/pc-geetest/register?t=" + (new Date()).getTime(), // 加随机数防止缓存
type: "get",
dataType: "json",
success: function (data) {
// 使用initGeetest接口
// 参数1:配置参数
// 参数