如何为django-simple-captcha创建Ajax刷新 [英] How to create Ajax refresh for django-simple-captcha
问题描述
我在基于django的网站上使用django-simple-captcha应用程序,我能够将captcha表单字段集成到表单中,但是问题是,如何创建一个按钮来调用Ajax刷新来刷新点击验证码图片?该应用程序的文档不是很清楚,我尝试按照文档中提供的示例进行操作,但是它不起作用。请帮我解决这个问题?
I'm using the django-simple-captcha app for my django based website, I am able to integrate the captcha form field into my form, but the problem is, how do I create a button which calls Ajax refresh to refresh the captcha image on click? The documentation for the app is not very clear, and I tried to follow the example given in the documentation but it doesn't work. Please help me on this issue?
编辑:这是django软件包的链接:
django-simple-captcha
Here's the link to the django package: django-simple-captcha
推荐答案
在javascript中实现:
Here's a working implementation in javascript:
$(function() {
// Add refresh button after field (this can be done in the template as well)
$('img.captcha').after(
$('<a href="#void" class="captcha-refresh">Refresh</a>')
);
// Click-handler for the refresh-link
$('.captcha-refresh').click(function(){
var $form = $(this).parents('form');
var url = location.protocol + "//" + window.location.hostname + ":"
+ location.port + "/captcha/refresh/";
// Make the AJAX-call
$.getJSON(url, {}, function(json) {
$form.find('input[name="captcha_0"]').val(json.key);
$form.find('img.captcha').attr('src', json.image_url);
});
return false;
});
});
然后,您只需要为 captcha-refresh
,也许将图像放在< a>
中,您就可以使用了!
Then you just need to add some CSS for the class captcha-refresh
, perhaps place an image in the <a>
and you're good to go!
这篇关于如何为django-simple-captcha创建Ajax刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!