当使用Recaptcha字段在同一页面上呈现两个表单时,只显示一个WTForms

时间:2014-06-17 23:11:38

标签: python recaptcha wtforms flask-wtforms

我正在使用带有WTforms的Flask。我也在使用WTFRecaptcha插件来使用Captcha字段。

原来我需要在同一页面上使用两种形式。当我在每个表单上分配验证码字段时,其中一个验证码不会在.html页面上呈现。这是因为验证码始终使用相同的ID创建:

我的forms.py文件上的Captcha和表单声明:

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

class FirstForm(Form):
    """First Form"""

    #Omitting fields here

    captcha_1 = RecaptchaField('Captcha', [], public_key='OMITTING_PUBLIC_KEY', private_key='OMITTING_PRIVATE_KEY', secure=True)

class Secondform(Form):
    """Second Form"""

    #Omitting fields here

    captcha_2 = RecaptchaField('Captcha', [], public_key='OMITTING_PUBLIC_KEY', private_key='OMITTING_PRIVATE_KEY', secure=True)

路线声明:

#!/usr/bin/env python

from flask import Flask, render_template, request
from flask.ext.assets import Environment
from forms import FirstForm, SecondForm
from flask import request
from flask import jsonify

@app.route('/test')
def test_form():
    """Test."""
    form_1 = FirstForm(request.form, captcha_1={'ip_address': request.remote_addr})
    form_2 = SecondForm(request.form, captcha_2={'ip_address': request.remote_addr})
    if request.method == 'POST' and (form_1.validate() or form_2.validate()) :
        return "Instructions have been sent to your e-mail"
     return render_template(
        'test-form.html',
        title='Get Started',
        form_1=form_1,
        form_2=form_2       
    )    

测试form.html

{% extends "base.html" %}

{% block content %}

    <div class="container block-form">
        <div class="row first">
            <div class="col-xs-12 col-md-7 border-right">
                <h1 class="title">{{ title }}</h1>
                <p>{{ description }}</p>
                <div class="form-area">
                    <form method="post">
                        {% for field in form_1 %}
                            <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") | safe }}
                                    </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-gradient">Submit</button>
                    </form>
                </div>
            </div>
        </div>

        <div class="row second">
            <div class="col-xs-12 col-md-7 border-right">
                <h1 class="title">{{ title }}</h1>
                <p>{{ description }}</p>
                <div class="form-area">
                    <form method="post">
                        {% for field in form_2 %}
                            <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") | safe }}
                                    </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-gradient">Submit</button>
                    </form>
                </div>
            </div>
        </div>

    </div>
{% endblock %}

为form_1中的验证码呈现的代码(直到div元素):

<script src="https://www.google.com/recaptcha/api/challenge?k=6LeCJvUSAAAAAAvqwJEueVdV0wyNLPtX6KWSTdXp" type="text/javascript">
//Other code here omitted
<script src="https://www.google.com/recaptcha/api/js/recaptcha.js" type="text/javascript">
//Other code here omitted
<div id="recaptcha_widget_div" class=" recaptcha_nothad_incorrect_sol recaptcha_isnot_showing_audio">

为form_2中的验证码呈现的代码(直到div元素):

<script type="text/javascript" src="https://www.google.com/recaptcha/api/challenge?k=6LeCJvUSAAAAAAvqwJEueVdV0wyNLPtX6KWSTdXp">
<script type="text/javascript" src="https://www.google.com/recaptcha/api/js/recaptcha.js"/>
<div id="recaptcha_widget_div" style="display: none;"/>
<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>

结果:只显示一个验证码。

...因此,如果我有两个验证码字段(可能在两种不同的表格上),则不会显示一个。

任何解决方案/建议?

2 个答案:

答案 0 :(得分:2)

这是Recaptcha

的一个记录良好的限制
  

目前,Google验证码机制每页只提供一个验证码表格

我建议您重新考虑组织网页的方式。 HTML中的表单设计简单。围绕它们构建的大多数工具都假定页面执行一项操作并在单个表单提交中将结果提交给服务器。

免责声明:我对您的代码一无所知。 无论如何继续进行:它闻起来像你的设计可能太聪明了。我的意思是,如果你还没有看到它在其他地方完成,谷歌的工具不支持它,问题可能在于你的方法。

如果需要提交单个无状态事务的结果,那么<form>是合适的,WTForms是生成它的一个很好的工具。如果你需要更丰富的东西,你可以考虑以下几点:

  • 将表单分成多个页面。一组简单的超链接可以提供易于导航的层次结构。
  • 使用javascript构建您的DOM并提交到RESTful端点(您甚至可以通过将请求主体转换为MultiDict并使用Recaptcha支持AJAX来使用WTForms进行验证)
  • 使用javascript动态构建<form>并切换action以对应服务器上正确的表单处理器。

答案 1 :(得分:0)

使用reCAPTCHA无法做到这一点。

请参阅相关的ASP.NET问题:Multiple reCAPTCHAs in one ASP.Net page

可能的解决方法:How do I show multiple recaptchas on a single page?

相关问题