- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将 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 是一个很好的生成它的工具。如果您需要更丰富的东西,您可以考虑以下内容:
MultiDict
来使用 WTForms 进行验证,并且 Recaptcha 支持 AJAX)<form>
使用 javascript 动态地切换 action
与您服务器上正确的表单处理器相对应。关于python - WTForms 在同一页面上呈现两个带有 Recaptcha 字段的表单时,仅显示一个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24274764/
我正在阅读 http://pythonhosted.org/WTForms/但它没有在任何地方定义过滤器函数的接口(interface),就像在 Field() 构造函数的 filters= 关键字参
在我的表单中,我尝试创建一个带有选择的自定义数组字段。 自定义表单字段: class CustomField(Field): widget = TextInput() def _val
我希望使用 Flask-WTForms 制作一个输入表单,其字段根据其他字段中的内容而变化。 例如,假设有一个“输入联系人”复选框。单击该按钮后,将出现(或激活)新字段,用于输入联系人的姓名和电话号码
这是我在 StackOverflow 上的第一篇文章,大家好。 我正在开发博客应用程序来学习 Python 和 Flask,我想在 Google App Engine 上启动它。不幸的是,我在将 WT
我正在尝试将数据从我的FlaskSQLAlChemy数据库自动填充到FlaskForm。以下是我的代码片段:。我想把这种型号的产品自动填充到一种可以接受购买的形式。以下是我的表格:。这是我的路线:。最
我使用的是最新版本的 flask、wtforms 和 Flask-WTForms。 我有一个显示表单的页面,其中一个是带有选项“A”的选择框。 应用程序启动时一切正常。在另一种形式中,我添加了一条名为
我有一个非常基本的 Flask 应用程序: from flask import Flask, render_template from flask_wtf import FlaskForm from
WTForms已更新为 2.3.0有了突破性的变化。我尝试调整其他要求,例如 werkzeug.1.0.0但还没有运气。有没有人找到解决办法? 这是堆栈跟踪: Traceback (most rece
在 WTForms 中,我可以根据数据库中的数据创建自定义验证器吗? 例如,我想显示一个多选字段,用户只能根据他们的帐户类型(存储在数据库中)选择一定数量的值。 预期的行为是这样的: 如果提交了表单,
我目前正在尝试使用 Flask 构建一个简单的 Web 应用程序。有了这个,我也在使用 WTForms,但是我在从表单中获取日期信息并对其进行验证时遇到了问题。 这是表格: from flask_wt
我有一个带有用户登录系统的 flask 应用程序。我现在注意到一些与用于注册和登录网站的电子邮件地址区分大小写相关的问题。 TL;DR 问题:我如何使用 Flask 表单的小写字母并使用它来评估用户是
我要求的其实很简单。我想创建一个包含一些字段和一个提交和一个取消按钮的表单。我想使用 Flask-Bootstrap 的 quick_form 模板函数来降低模板中的开销。我的表格是这样的: from
可能有更好的方法来执行此操作,但我正在尝试使用隐藏表单在 Flask 中使用 WTForms 定义按钮。当我尝试设置值时,这些字段似乎生成了两次。一次使用默认值“”,一次使用模板中设置的值。最终,在发
是否可以在离开字段后但在提交之前验证 WTForm 字段? 例如,在输入用户名后,在用户单击提交之前,验证该字段以查看其是否可用并显示复选标记。 最佳答案 当字段更改时,执行检查并更改相邻节点中的文本
我发现 IntegerField 在为空时不验证。要重现,请复制以下 block : from flask import Flask, render_template_string from flas
我需要将一个对象从我的数据库传递到我的表单,我想从中读取默认值。我当前的代码如下所示: 我的初始化: form = EditEventForm(event) 还有我的表单类: class EditEv
我正在使用 Flask 和 Flask-WTF,我需要创建一个包含多个具有相似结构的 block (子表单)的表单(例如具有一个 SelectField 和一个 TextAreaField 的子表单)
我需要将一个对象从我的数据库传递到我的表单,我想从中读取默认值。我当前的代码如下所示: 我的初始化: form = EditEventForm(event) 还有我的表单类: class EditEv
已经看过this答案,我无法让我的自定义 WTForm 验证器接受另一个参数。 我的代码的结构是我有一个 form_create() 方法,通过以下方式调用: form = other_fil
我在 Web 应用程序中使用 Python 3.7.4 以及 Flask 1.1.1 和 WTforms 2.2.1。 我正在尝试创建DateField像这样: user_birth = DateFi
我是一名优秀的程序员,十分优秀!