如何在提交时清除WTF表格中的值?

时间:2018-01-15 01:36:48

标签: python flask psycopg2 wtforms

我有两个关于烧瓶和重量的问题:

  1. 提交后存储在表单中输入的值在哪里?

  2. 我如何在提交时清除这些值?

  3. 目前,我的表单已成功提交输入值,但它们会保留在某处。因此,当我提交表单然后重新加载页面时,值将再次提交。

    以下是python代码的相关片段。它仍然是WIP,所以你会看到一堆不合逻辑的东西。

    from wtforms import Form
    from wtforms import StringField, PasswordField, BooleanField, SelectField, DecimalField, SubmitField
    from wtforms import validators
    
    class WeightForm(Form):
        num = DecimalField(u'Weight (lb):', [validators.number_range()])
        note = StringField(u'Notes:')
        submit = SubmitField(u'Submit')
    
    
    def actvityLogQuery():
        query = """ INSERT INTO public.activity_log (user_id, activity_type_id, effective_date, numerical, notes)
                VALUES (%s,%s,%s,%s,%s);"""
        return query
    
    
    @app.route('/activitylog/', methods=['GET', 'POST'])
    def actvityLogPage():
        cur, conn = connection()
        weightForm = WeightForm(request.form)
        if request.method == "POST" and weightForm.validate():
            num = weightForm.num.data
            note = weightForm.note.data
            unow = datetime.utcnow()
            Q = actvityLogQuery()
            cur.execute(Q, (13, 1, unow, num, note))
    
            conn.commit()
            flash("Activity entered successfully")
            cur.close()
            conn.close()
            gc.collect()
    
        return render_template('activityLog.html', weightForm=weightForm)
    

    HTML文件的相关摘要:

    <div class="tab-pane fade in active" role=tabpanel id=weight aria-labelledby=weight-tab>
                       <div class="container">
                            <h4>Log Weight:</h4>
                            <br/>
                            {% from "_formhelpers.html" import render_field %}
                            <form method=post action="/activitylog/">
                                <dl>
                                    <h6>{{render_field(weightForm.num)}}</h6>
    
                                    <h6>{{render_field(weightForm.note)}}</h6>
                                </dl>
                                <p><input type=submit value=Enter></p>
                            </form>
    
                      {% if error %}
                            <p class="error"><strong>Error:</strong>{{error}}</p>
                      {% endif %}
                      </div>
                </div>
    

    谢谢!

0 个答案:

没有答案