Flask WTForms未提交,但我没有收到任何错误

时间:2018-11-02 12:33:50

标签: python flask

我在使用WTForms的单个页面中有2个表单。第一个按预期工作并将数据提交到数据库(Mysql)。第二个什么也不做,但是我也没有错误...

第二个表单应有出现,数据验证也应有作用。数据输入看起来正常,但是当我点击提交页面时,页面会重新加载。数据库工作正常(使用插入语句输入与页面相同的值)。

我在app.py中的表格

 # Article Form Class
class ArticleForm(Form):
    title = StringField('Title', [validators.Length(min=1, max=200)])
    body = TextAreaField('Body', [validators.Length(min=30)])

# Add Article
@app.route('/add_article', methods=['GET', 'POST'])
@is_logged_in
def add_article():
    form = ArticleForm(request.form)
    if request.method == 'POST' and form.validate():
        title = form.title.data
        body = form.body.data

        # Create Cursor
        cur = mysql.connection.cursor()

        # Execute
        cur.execute("INSERT INTO articles(title, body, author) VALUES(%s, %s, %s)",(title, body, session['username']))

        # Commit to DB
        mysql.connection.commit()

        #Close connection
        cur.close()

        flash('Article Created', 'success')

        return redirect(url_for('dashboard'))

    return render_template('add_article.html', form=form)

# Eisigisi Form Class
class EisigisiForm(Form):
    ey = StringField('Ey', [validators.Length(min=1, max=200)])
    eisigisitype = StringField('Eisigisitype', [validators.Length(min=10)])
    dateds = DateField('Dateds')
    ergoid = IntegerField('Ergoid', [validators.DataRequired()])
    ergotitle = StringField('Ergotitle', [validators.Length(min=5, max=200)])
    team = StringField('Team',[validators.Length(min=30)])
    notes = StringField('Notes', [validators.Length(min=30)])




# Add Eisigisi
@app.route('/add_eisigisi', methods=['GET', 'POST'])
@is_logged_in
def add_eisigisi():
    form = EisigisiForm(request.form)
    if request.method == 'POST' and form.validate():
        ey = form.ey.data
        eisigisitype = form.eisigisitype.data
        dateds = form.dateds.data
        ergoid = form.ergoid.data
        ergotitle = form.ergotitle.data
        team = form.team.data
        notes = form.notes.data

        # Create Cursor
        cur = mysql.connection.cursor()

        # Execute
        cur.execute("INSERT INTO eisigiseis(ey, eisigisitype, dateds, ergoid, ergotitle, team, notes) VALUES(%s, %s, %s, %s, %s, %s, %s)",(session['username'], eisigisitype, dateds, ergoid, ergotitle, team, notes))

        # Commit to DB
        mysql.connection.commit()

        #Close connection
        cur.close()

        flash('Eisigisi Created', 'success')

        return redirect(url_for('home'))

    return render_template('add_eisigisi.html', form=form)

我的第一种表单HTML:

{% extends 'layout.html' %}

{% block body %}
  <h1>Εισαγωγή Ομάδας</h1>
  {% from "includes/_formhelpers.html" import render_field %}
  <form method="POST" action="{{url_for('add_article')}}">
    <div class="form-group">
      {{ render_field(form.title, class_="form-control") }}
    </div>
    <div class="form-group">
      {{ render_field(form.body, class_="form-control", id="editor") }}
    </div>
    <p><input class="btn btn-primary" type="submit" value="Submit">
  </form>
{% endblock %}

我的第二HTML:

{% extends 'layout.html' %}

{% block body %}
  <h1>Add Eisigisi</h1>
  {% from "includes/_formhelpers.html" import render_field %}
  <form method="POST" action="{{url_for('add_eisigisi')}}">
    <div class="form-group">
      {{ render_field(form.eisigisitype, class_="form-control") }}
    </div>
    <div class="form-group">
      {{ render_field(form.dateds, class_="form-control") }}
    </div>
    <div class="form-group">
      {{ render_field(form.ergoid, class_="form-control") }}
    </div>
    <div class="form-group">
      {{ render_field(form.ergotitle, class_="form-control") }}
    </div>
    <div class="form-group">
      {{ render_field(form.team, class_="form-control") }}
    </div>
    <div class="form-group">
      {{ render_field(form.notes, class_="form-control")}}
    </div>
    <p><input class="btn btn-primary" type="submit" value="Submit">
  </form>
{% endblock %}

这些是来自Chrome请求标头的响应

 Request URL: http://localhost:5000/add_eisigisi
    Request Method: POST
    Status Code: 200 OK
    Remote Address: 127.0.0.1:5000
    Referrer Policy: no-referrer-when-downgrade
    Content-Length: 3080
    Content-Type: text/html; charset=utf-8
    Date: Mon, 05 Nov 2018 12:19:32 GMT
    Server: Werkzeug/0.14.1 Python/2.7.15rc1
    Vary: Cookie
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
    Accept-Encoding: gzip, deflate, br
    Accept-Language: en-US,en;q=0.9
    Cache-Control: max-age=0
    Connection: keep-alive
    Content-Length: 242
    Content-Type: application/x-www-form-urlencoded
    Cookie: session=eyJsb2dnZWRfaW4iOnRydWUsInVzZXJuYW1lIjoibWFub3MifQ.W-A01g.kStd6TFosoCUU7txirBKOl6URVI
    Host: localhost:5000
    Origin: http://localhost:5000
    Referer: http://localhost:5000/add_eisigisi
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36
    eisigisitype: auti einai mia eisigisi
    dateds: 2018-01-01
    ergoid: 63098700
    ergotitle: autow einai enas titlos ergou eyropaikou
    team: manos, panos, vivi, giota, eugeunia, mirsini
    notes: manos, panos, vivi, giota, eugeunia, mirsini

1 个答案:

答案 0 :(得分:1)

最后!经过几个小时的查看代码,我找到了解决方案!

ey = form.ey.data

上面的行引起了问题。 ey是我从会话cookie中获取的数据,因此没有理由在表单中添加字段。谢谢大家!