我正在尝试使用wtforms,flask和bootstrap来创建一个简单的表单,但是我的表单提交会导致使用params而不是POST的GET。
我只用烧瓶就可以了,但是当我切换到从flask.ext.wtf导入Form时,使用form.validate_on_submit而不是仅仅验证,并使用flask_bootstrap,但现在提交表单会导致GET操作而不是一个POST动作。我错过了什么?
这是我在控制台中看到的内容:
GET
127.0.0.1 - - [14/Apr/2014 21:04:10] "GET / HTTP/1.1" 200 -
GET
127.0.0.1 - - [14/Apr/2014 21:04:13] "GET /?csrf_token=None&recipe1=dfsaasdf&recipe2=adfsadfs&submit_button=Submit+Form HTTP/1.1" 200
这是我的应用程序的全部内容:
from flask import render_template, request, redirect, url_for
from flask.ext.wtf import Form
from wtforms.ext.csrf import SecureForm
from wtforms import TextField, HiddenField, ValidationError, RadioField,\
BooleanField, SubmitField, IntegerField, FormField, validators
from concat.recipe_concatenator import RecipeConcatenator
from flask_bootstrap import Bootstrap
from flask import Flask
from hashlib import md5
app = Flask(__name__)
Bootstrap(app)
app.config['DEBUG'] = True
SECRET_KEY = '1234567890'
class RecipeCompareForm (Form):
recipe1 = TextField('Recipe 1', [])
recipe2 = TextField('Recipe 2')
submit_button = SubmitField('Submit Form')
@app.route('/', methods=['GET', 'POST'])
def compare():
print request.method
form = RecipeCompareForm(csrf_enabled=False)
if form.validate_on_submit():
print "validated"
print form.recipe1.data
message = "blah"
return redirect(url_for('results', message=message))
return render_template('form.html', form=form)
@app.route('/results')
def results():
message = request.args['message']
return render_template('results.html', message=message)
if __name__ == '__main__':
app.run()
模板:
{% extends "bootstrap/base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% import "bootstrap/fixes.html" as fixes %}
{% block content %}
<form class="form form-horizontal" role ="form">
{{ form.hidden_tag() }}
{{ wtf.form_errors(form, hiddens="only") }}
{{ wtf.form_field(form.recipe1) }}
{{ wtf.form_field(form.recipe2) }}
{{ wtf.form_field(form.submit_button) }}
</form>
{% endblock %}
答案 0 :(得分:3)
如果您未指定method
属性,则表单默认为GET
。要使用POST
,请将method属性添加到表单标记中,如下所示:
<form class="form form-horizontal" role="form" method="POST">