2

I have two questions with respect of flask and wtforms:

  1. Where are the values entered in the form stored after submission?

  2. How can i clear those values upon submission?

Currently, my form successfully submits the input values, but they are retained somewhere. So, when I submit the form and then I reload the page, the values are submitted a second time.

Below are the relevant snippets of the python code. It is still WIP, so you will see a bunch of illogical things.

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)

The relevant snippet of the HTML file:

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

Thanks!

assenuma
  • 93
  • 7

0 Answers0