views:

67

answers:

0

Weird CAPTCHA behavior. If failed, CAPTCHA hijacks page.

When submitting the form with errors, the correct page (displaying what fields are incorrect) is displayed for a brief moment then is immediately hijacked by the CAPTCHA form leaving only the CAPTCHA form on the page.

The CAPTCHA does not redirect to a new URL, the URL stays the same.

I didn't implement this feature into the Python/Django project myself so I'm not sure how to stop this behavior.

I'm not sure which code is relevant so I'll wait for suggestions before adding code, so just make a request.

Code: views, widgets, forms

def data_page(request, data_slug):
        data = get_object_or_404(Data, slug=data_slug)
        recaptcha_public_key = settings.RECAPTCHA_PUBLIC_KEY
        websnapr_key = settings.WEBSNAPR_API_KEY
        return render_to_response('data_page.html',
                                  {'data': data,
                                   'recaptcha_public_key': recaptcha_public_key,
                                   'websnapr_key': websnapr_key},
                                   context_instance=RequestContext(request))

class ReCaptcha(forms.widgets.Widget):
    recaptcha_challenge_name = 'recaptcha_challenge_field'
    recaptcha_response_name = 'recaptcha_response_field'

def render(self, name, value, attrs=None):
    return mark_safe(u'%s' % captcha.displayhtml(settings.RECAPTCHA_PUBLIC_KEY))

def value_from_datadict(self, data, files, name):
    return [data.get(self.recaptcha_challenge_name, None), 
        data.get(self.recaptcha_response_name, None)]

class DataCommentForm(CommentForm):
    country = forms.ChoiceField(choices=COUNTRIES)
    recaptcha = ReCaptchaField()

def get_comment_model(self):
    return DataComment

def get_comment_create_data(self):
    data =  super(DataCommentForm, self).get_comment_create_data()
    data['country'] = self.cleaned_data['country']
    return data