如何为django-simple-captcha创建Ajax刷新

csh*_*bie 6 django captcha

我在基于django的网站上使用django-simple-captcha应用程序,我能够将captcha表单字段集成到我的表单中,但问题是,如何创建一个调用Ajax刷新刷新验证码图像的按钮点击?该应用程序的文档不是很清楚,我试图按照文档中给出的示例,但它不起作用.请帮我解决这个问题?

编辑:这是django包的链接: django-simple-captcha

MW.*_*MW. 19

这是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;
    });
});
Run Code Online (Sandbox Code Playgroud)

然后你只需要为课程添加一些CSS captcha-refresh,也许放置一个图像<a>,你就可以了!