2014-06-12 75 views
1

我正在使用wtforms-recaptcha來獲取顯示的Recaptcha。帶WTforms的RECaptcha將不會呈現

pip install wtforms-recaptcha 

我自己引導本網站使安裝:

https://pypi.python.org/pypi/wtforms-recaptcha 

的問題是的ReCaptcha代碼被迴盪到表單中。也就是說,我看到了驗證碼的代碼的形式,而不是對的ReCaptcha本身:

<script type="text/javascript" src="https://www.google.com/recaptcha/api/challenge?k=6LeCJvUSAAAAAAvqwJEueVdV0wyNLPtX6KWSTdXp"> </script> <noscript> <iframe src="https://www.google.com/recaptcha/api/noscript?k=6LeCJvUSAAAAAAvqwJEueVdV0wyNLPtX6KWSTdXp" 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> 

表單代碼上form.py:

from wtforms import PasswordField, StringField, validators, widgets 
from wtforms.form import Form 
from wtfrecaptcha.fields import RecaptchaField 

class ContactForm(Form): 
"""Enables the user to provide feedback.""" 

first_name = StringField('First Name', [ 
    validators.DataRequired() 
]) 
last_name = StringField('Last Name', [ 
    validators.DataRequired() 
]) 
captcha = RecaptchaField('Captcha', [], public_key='6LeCJvUSAAAAAAvqwJEueVdV0wyNLPtX6KWSTdXp', private_key='6LeCJvUSAAAAADcUvYyLv8kt9ARiTAluDGqHBumY', secure=True) 

從HTML中調用形式:

   <form method="post"> 
       {% for field in form %} 
        <div class="form-group{% if field.errors %} has-error has-feedback{% endif %}"> 
         <div class="row"> 
          <div class="col-xs-12 col-md-4"> 
           {{ field.label(class="control-label") }} 
          </div> 

          <div class="col-xs-12 col-md-8"> 
           {{ field(class="form-control") }} 
          </div> 
         </div> 
         {% if field.errors %} 
          <span class="glyphicon glyphicon-remove form-control-feedback"></span> 
         {% endif %} 
         {% for error in field.errors %} 
          <p class="help-block text-danger"> 
           <span class="glyphicon glyphicon-remove"></span> 
           {{ error }} 
          </p> 
         {% endfor %} 
        </div> 
       {% endfor %} 
       <br> 
       <button type="submit" class="btn btn-primary">{{ title }}</button> 
      </form> 

路由調用代碼:

@app.route('/contact', methods=['GET', 'POST']) 
def contact(): 
"""Display the contact page.""" 
form = ContactForm(request.form, captcha={'ip_address': request.remote_addr}) 
if request.method == 'POST' and form.validate(): 
    return "Thank you for contacting us." 
return render_template(
    ... 
) 

回答

3

問題是WTForms-RECAPTCH不返回safe字符串,而是返回一個unicode字符串。潛在問題需要修正here(通過返回wtforms.widgets.core.HTMLString或其他提供__html__方法的實例)。

要解決的問題,因爲現在你應該簡單地標記字段safe在你的模板:

<div class="col-xs-12 col-md-8"> 
    {{ field(class="form-control") | safe }} 
</div> 

或者交替,只標出再驗證碼字段作爲安全:

<div class="col-xs-12 col-md-8"> 
    {% if field.short_name == "captcha" %} 
     {{ field(class="form-control") | safe }} 
    {% else %} 
     {{ field(class="form-control") }} 
    {% endif %} 
</div> 

a PR for this issue,這是固定的版本0.3.2

+0

我從來不會想到這一點。非常感謝! – Dynelight

+0

剛剛發佈了包含Sean修補程序的pypi上的新版本。再次感謝您注意並修復此問題! – excieve