在烧瓶中验证 reCAPTCHA

Validating for reCAPTCHA in flask

我在我的网站上制作了一个错误表单,它仍然可以正常工作,除了验证码有点傻。如果我填写文本中的所有字段而不只是填写验证码,它仍会在没有警告或任何情况下发送给我,我在那里设置的验证将不会响应,我需要帮助它。

HTML代码:

<form action="{{ url_for('get_contact') }}" method=post>
            <div>
                <p>
                    {{ form.csrf_token }}
                    {{ form.name.label }} <br>
                    {{ form.name(placeholder='Jméno') }}
                    <ul>
                        {% for error in form.name.errors %}
                            <li style="color:red;">{{ error }}</li>
                        {% endfor %}
                    </ul>
                <p>
                    {{ form.email.label }} <br>
                    {{ form.email(placeholder='Email') }}
                    <ul> 
                        {% for error in form.email.error %}
                           <li style="color:red;">{{ error }}</li> 
                        {% endfor %}
                        </ul>
                </p>
                <p>
                    {{ form.message.label }}<br>
                    {{ form.message(placeholder='Zpráva') }}
                </p>
                <p>
                    {{ form.recaptcha }}
                    {% for error in form.recaptcha.errors %}
                    <ul>
                        <li>{{ error }}</li>
                    {% endfor %}
                    </ul>
                    <input class="contact-submit" type="submit" value="Submit">

烧瓶代码:

@app.route('/contact', methods=["GET","POST"])
def get_contact():
    form = ContactForm()
    if request.method == 'POST':
        name =  request.form["name"]
        email = request.form["email"]
        message = request.form["message"]
        res = pd.DataFrame({'name':name, 'email':email, 'message':message}, index=[0])
        res.to_csv('./contactDatabase.csv', mode='a', header =False)
        return redirect(url_for("rgb"))
    else:
        return render_template('contact.html', form=form)

Flask 形式代码:

class ContactForm(FlaskForm):
    name = TextField("Jméno", [validators.DataRequired(), validators.Length(max=255)])
    email = StringField("Email", [validators.DataRequired(), validators.Length(min=6, max=35)])
    message = TextAreaField("Zpráva")
    recaptcha = RecaptchaField()
  1. 您可能忘记使用 form.validate_on_submit()
@app.route('/contact', methods=["GET","POST"])
def get_contact():
    form = ContactForm()
    if request.method == 'POST' and form.validate_on_submit():
        name =  form.name
        email = form.email
        message = form.message
        res = pd.DataFrame({'name':name, 'email':email, 'message':message}, index=[0])
        res.to_csv('./contactDatabase.csv', mode='a', header =False)
        return redirect(url_for("rgb"))
    else:
        return render_template('contact.html', form=form)

我没有测试上面的代码,所以我不知道它是否有效。

  1. 检查 app.testing 是否为 True1

flask-wtforms documentation 指出:

For your convenience, when testing your application, if app.testing is True, the recaptcha field will always be valid.

  1. 此外,不要忘记您必须使用从您的 ReCaptcha 注册中收到的相应 public 和私钥设置 RECAPTCHA_PUBLIC_KEYRECAPTCHA_PRIVATE_KEY 配置变量。

1 如果直接设置为True或者设置TESTING就returnsTrueTrue.