gpt4 book ai didi

python - 带有 WTforms 的 RECaptcha 不会呈现

转载 作者:行者123 更新时间:2023-11-30 23:23:00 25 4
gpt4 key购买 nike

我正在使用 wtforms-recaptcha 来显示 Recaptcha。

pip install wtforms-recaptcha

我在这个网站上引导自己进行安装:

https://pypi.python.org/pypi/wtforms-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(
...
)

最佳答案

问题在于 WTForms-RECAPTCH 不返回 safe 字符串,而是返回 unicode 字符串。需要修复根本问题here (通过返回 wtforms.widgets.core.HTMLString 的实例或提供 __html__ 方法的其他内容)。

要暂时解决此问题,您只需在模板中将该字段标记为安全即可:

<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 版本中已修复

关于python - 带有 WTforms 的 RECaptcha 不会呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24189004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com