2013-05-25 123 views
1

我試圖在我的窗體上實現Django reCAPTCHA。問題是,它沒有驗證每次reCAPTCHA會話,我正確地提交表單。我試圖提交reCAPTCHA表格大約30次,並且仍然不驗證reCAPTCHA。Django reCAPTCHA不驗證響應

我在開發服務器上實現了reCAPTCHA,並將域名添加爲www.example.com。

我還使用本教程http://www.chrisumbel.com/article/recaptcha_with_django

這可能是原因所在,它不會因爲驗證,我使用的發展,否則有人可以幫我嗎?

from django.contrib.auth.views import password_reset 
from django.shortcuts import render 
from recaptcha.client import captcha 


def forgot_password(request): 
    if request.method == 'POST': 
     response = captcha.submit(request.POST.get('recaptcha_challenge_field'), request.POST.get('recaptcha_response_field'), '1231d12dsad12', request.META['REMOTE_ADDR'],) 


     if response.is_valid: 
      captcha_response = "YOU ARE HUMAN: %(data)s" % {'data' : edit_form.data['data_field']} 
     else: 
      captcha_response = 'YOU ARE ROBOT' 
      return render(request, 'forgot_password.html',{'captcha_response':captcha_response}) 



    else: 
     return render(request, 'forgot_password.html') 

模板

{% block title %}Forgot Password<br>{% endblock title %} 


<form method="post" action="{% url accounts:forgot-password %}"> 
    {% csrf_token %} 
    <p>Please enter your email address. 
     You will receive a link to create a new password via email.</p> 

    <input type="email" name="email" 
     placeholder="Your e-mail"><br/> 



     <th>Are you human?</th> 

      <span class="validation_error">{{ captcha_response }}</span> 

      <script type="text/javascript" 
      src="http://api.recaptcha.net/challenge?k=123dwqsdasd123e23d32d32"> 
      </script> 

      <noscript> 
      <iframe src="http://api.recaptcha.net/noscript?k=123dwqsdasd123e23d32d32"> 
      height="300" width="500" frameborder="0"></iframe><br> 
      <textarea name="recaptcha_challenge_field" rows="3" cols="40"> 
      </textarea> 
      <input type="hidden" name="recaptcha_response_field" 
      value="manual_challenge"> 
      </noscript> 


       <button type="submit">Send new password</button> 

</form> 
+0

嘗試使您的reCaptcha鍵全局化,如果您尚未完成。 –

+0

@Piotr Jaszkowski你能解釋一下更多關於製作全球關鍵 – donkeyboy72

+0

啊對不起,你不能改變關鍵是全球性的。但是,您可以在此網站上啓用全局選項並創建新的:https://www.google.com/recaptcha/admin/create –

回答

2

你可以做自己的實現:

import urllib, urllib2, re 

def recaptcha(request, postdata): 
     rc_challenge = postdata.get('recaptcha_challenge_field','') 
     rc_user_input = postdata.get('recaptcha_response_field', '').encode('utf-8') 
     url = 'http://www.google.com/recaptcha/api/verify' 
     values = {'privatekey' : 'PRIVATE-KEY', 'remoteip': request.META['REMOTE_ADDR'], 'challenge' : rc_challenge, 'response' : rc_user_input,} 
     data = urllib.urlencode(values) 
     req = urllib2.Request(url, data) 
     response = urllib2.urlopen(req) 
     answer = response.read().split()[0] 
     response.close() 
     return answer 

時的驗證碼是正確鍵入此返回true否則爲假。

在你看來,你可以那麼做這樣的事情:

if request.method == "POST": 
    postdata = request.POST.copy() 
    captcha = recaptcha(request, postdata) 
    if captcha: 
     #do something 
    else: 
     #do something else 

你將不得不調整自己的模板了一下,不應該是很難弄明白。希望這導致正確的方向。

+0

我可以問,我需要做什麼調整?我一直在學習django只有幾個月:( – donkeyboy72

+0

嗯,也許它的工作原理就像你已經得到它。{captcha_response}}應該從你的視圖呈現,因爲我看到。你也可以改變JavaScript指向谷歌api('')只要試一下,看看有多遠 – Jingo

+0

謝謝jingo – donkeyboy72