How to create Ajax refresh for django-simple-captcha

前端 未结 2 1900
天命终不由人
天命终不由人 2021-01-05 16:17

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 butto

2条回答
  •  别那么骄傲
    2021-01-05 16:23

    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(
                $('Refresh')
                );
    
        // 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;
        });
    });
    

    Then you just need to add some CSS for the class captcha-refresh, perhaps place an image in the and you're good to go!

提交回复
热议问题