gpt4 book ai didi

python - WTForms 在同一页面上呈现两个带有 Recaptcha 字段的表单时,仅显示一个表单

转载 作者:行者123 更新时间:2023-12-01 05:10:48 26 4
gpt4 key购买 nike

我正在将 Flask 与 WTforms 一起使用。我还使用 WTFRecaptcha 插件来使用验证码字段。

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

我的 forms.py 文件上的验证码和表单声明:

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
)

测试表单.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>

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

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

有什么解决方案/建议吗?

最佳答案

这是 Recaptcha 有据可查的限制

Currently, the Google captcha mechanism offer only one captcha form per page

我鼓励您重新考虑组织页面的方式。 HTML 中的表单设计得很简单。围绕它们构建的大多数工具都假设页面执行一件事并通过单个表单提交将结果提交到服务器。

免责声明:我对您的代码一无所知。 无论如何继续:听起来你的设计可能太聪明了。我的意思是,如果您没有在其他地方看到过它,并且谷歌的工具不支持它,那么问题可能出在您的方法上。

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

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

关于python - WTForms 在同一页面上呈现两个带有 Recaptcha 字段的表单时,仅显示一个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24274764/

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