Вопрос

I am trying to get a simple form to work using wtforms, flask, and bootstrap, but my form submission results in a GET with the form params instead of a POST.

I got it work with just flask, but when I switched to importing Form from flask.ext.wtf, using form.validate_on_submit instead of just validate, and using flask_bootstrap, but now submitting the form results in a GET action instead of a POST action. What am I missing?

Here's what I am seeing in my console:

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

Here's my app in its entirety:

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()

The template:

{% 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 %}
Это было полезно?

Решение

If you don't specify a method attribute, forms default to GET. To use POST, add the method attribute to your form tag, like this:

<form class="form form-horizontal" role="form" method="POST">
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top