2011-12-05 23 views
2

我在網站上放置了一個Django應用程序,但我手動呈現這些表單(即實際輸入每個字段並使用AJAX提交它們)。Django中的Recaptcha沒有窗體?

如何將Recaptcha整合到我的表單中?謝謝您的幫助!

+0

這個問題之前(http://stackoverflow.com/questions/1526399/django-forms-wizard-and-recaptcha)有很多選項。 –

回答

1

我只是用這條巨蟒客戶端驗證碼:

http://pypi.python.org/pypi/recaptcha-client

然後我的看法是這樣的:

captcha_key = get_config('RECAPTCHA_PUB_KEY',None) 
recaptcha_challenge_field = request.POST.get('recaptcha_challenge_field', None) 
recaptcha_response_field = request.POST.get('recaptcha_response_field', None) 
check_captcha = captcha.submit(recaptcha_challenge_field, recaptcha_response_field, settings.RECAPTCHA_PRIVATE_KEY, request.META['REMOTE_ADDR']) 
if check_captcha.is_valid is False: 
    log.info('captcha_error : %s' % check_captcha.error_code) 
    return {'TEMPLATE':template_name,'captcha_error': True,'register_form': f,'captcha_key':captcha_key ,'next':redirect_to} 
2

驗證碼,客戶端不與python3工作。我結束了使用django-recaptcha(https://pypi.python.org/pypi/django-recaptcha/1.0)。簡要文檔解釋瞭如何使用formfield 'ReCaptchaField' 使用reCAPTCHA,但你可以只使用提交功能從captcha.client這樣的:

import captcha.client 

[...]

recaptcha_response = captcha.client.submit( 
    request.POST.get('recaptcha_challenge_field'), 
    request.POST.get('recaptcha_response_field'), 
    '[[privatekey]]', 
    request.META['REMOTE_ADDR'],) 

然後你檢查是否recaptcha_response.is_valid

無需將recaptcha添加到您的INSTALLED_APPS或任何東西。