django-recaptcha返回“错误:缺少必需的参数:sitekey”

时间:2018-08-20 00:38:50

标签: django django-recaptcha

我按照django-recaptcha的指示向我的联系表添加了一个字段。呈现时(在我的本地测试中),它的字段显示为“ Captcha:”,但未呈现任何实际的验证码。 chrome中的开发者控制台说

 Uncaught Error: Missing required parameters: sitekey

https://www.gstatic.com/recaptcha/api2/v1531759913576/recaptcha__en.js

This answer说要向JavaScript中添加render=explicit,但是a)我不确定这甚至是问题,b)我认为Django包会处理它。

Django 1.11.8,Django recaptcha 1.4.0。

Django表单是

class ContactForm(forms.Form):
    contacter = forms.EmailField(required=True,
                                 label=_('Your Email (optional)'),
                                 widget=forms.widgets.TextInput(
                                     attrs={'size': '50'}))
    contact_text = forms.CharField(required=True,
                                   widget=forms.widgets.Textarea(
                                       attrs={'rows': '10',
                                              'cols': '70',
                                              'class': 'defaultText',
                                              'title':
                                              _('Type a message here')}))
    captcha = ReCaptchaField()

呈现的Django表单如下。

  <form action="/contact/"
        method="post" id="new-contact">
    <input type='hidden' name='csrfmiddlewaretoken' value='...' />

    <fieldset id="contact">
      <legend>Contact Us</legend>
      <tr><th><label for="id_contacter">Your Email (optional):</label></th><td><input type="text" name="contacter" required id="id_contacter" size="50" /></td></tr>
<tr><th><label for="id_contact_text">Contact text:</label></th><td><textarea name="contact_text" rows="10" title="Type a message here" id="id_contact_text" required cols="70" class="defaultText">
</textarea></td></tr>
<tr><th><label for="id_captcha">Captcha:</label></th><td><script src="https://www.google.com/recaptcha/api.js?hl=en"></script>
<div class="g-recaptcha" data-sitekey="" data-required="True" data-id="id_captcha" ></div>
<noscript>
  <div style="width: 302px; height: 352px;">
    <div style="width: 302px; height: 352px; position: relative;">
      <div style="width: 302px; height: 352px; position: absolute;">
        <iframe src="https://www.google.com/recaptcha/api/fallback?k="
                frameborder="0" scrolling="no"
                style="width: 302px; height:352px; border-style: none;">
        </iframe>
      </div>
      <div style="width: 250px; height: 80px; position: absolute; border-style: none;
                  bottom: 21px; left: 25px; margin: 0px; padding: 0px; right: 25px;">
        <textarea id="g-recaptcha-response" name="g-recaptcha-response"
                  class="recaptcha_challenge_field"
                  style="width: 250px; height: 80px; border: 1px solid #c1c1c1;
                         margin: 0px; padding: 0px; resize: none;" value="">
        </textarea>
        <input type="hidden" name="recaptcha_response_field" value="manual_challenge" />
      </div>
    </div>
  </div>
</noscript></td></tr>
    </fieldset>

    <input type="submit" value="Submit" />
  </form>

1 个答案:

答案 0 :(得分:0)

我也提出了这个问题here

按照指示将RECAPTCHA_PRIVATE_KEY和RECAPTCHA_PUBLIC_KEY设置为空字符串会产生此错误。相反,如果完全未设置这些变量(在dev环境的Django设置中),则库将使用测试键,一切正常。