如何防止此表单数据重新填充?

时间:2015-12-12 02:03:42

标签: python-2.7 flask flask-wtforms

我刚刚完成了一个简单的数字猜测网页游戏,告诉用户他们的数字是高于还是低于解决方案。

在向网页提交数据时,页面会正确更新游戏数据,但文本输入字段中填入之前刚刚提交的数据。在进行另一次猜测之前必须删除字段中的文本很烦人,我更喜欢它是否使用空表单字段呈现模板。

我以前使用redirect而不是render_template来解决这个问题,但在这种情况下我需要render_template将数据传递给页面。

此外,对我的设计的任何一般性建议都会受到赞赏,我对此很新。

这是页面功能:

@app.route('/numbers', methods=['GET', 'POST'])
def numbers():
    x = open(os.path.join(basedir, 'textdata/nums.txt'), 'r')
    numdata = x.readlines()
    solved = True if numdata[0] == 'Solved\n' else False
    tries = int(numdata[1][:-1])
    answer = int(numdata[2])
    x.close()
    form = NumForm()
    newform = NewNumForm()
    if newform.validate_on_submit() and request.form['btn'] == 'New Game':
        randnum = randint(1, 100)
        x = open(os.path.join(basedir, 'textdata/nums.txt'), 'w')
        x.write('Unsolved\n0\n' + str(randnum))
        x.close()
        return redirect('/numbers')
    if form.validate_on_submit() and request.form['btn'] == 'Submit':
        num = form.num.data
        if num not in [str(i) for i in range(1, 101)]:
            return render_template('numbers.html', form=form, ttl='Numbers',
                                   tries = tries, wrongin = True)
        tries += 1
        if int(num) == answer:
            x = open(os.path.join(basedir, 'textdata/nums.txt'), 'w')
            x.write('Solved\n' + str(tries) + '\n' + str(answer))
            x.close()
            return render_template('numend.html', form=newform, ttl='Numbers',
                                   tries = tries, justsolved = True,
                                   answer = answer)
        if int(num) > answer:
            greater = False
        if int(num) < answer:
            greater = True
        x = open(os.path.join(basedir, 'textdata/nums.txt'), 'w')
        x.write('Unsolved\n' + str(tries) + '\n' + str(answer))
        x.close()
        return render_template('numbers.html', form=form, ttl='Numbers',
                               tries = tries, greater = greater, num = num)

    if solved:
        return render_template('numend.html', form=newform, ttl='Numbers',
                               tries = tries, answer = answer)
    return render_template('numbers.html', form=form, ttl='Numbers',
                           tries = tries)
class NumForm(Form):
    num = StringField('num')

class NewNumForm(Form):
    pass

页面模板:

{% extends "base.html" %}
{% block content %}
    <br>
    <br><form action="" method="post" name="numform">
        {{ form.hidden_tag() }}
        <p>
            {% if num %}
            {% if greater %}
            The number is greater than {{ num }}
            {% else %}
            The number is smaller than {{ num }}
            {% endif %}
            {% endif %}
            <h2>Guess the number! (between 1 and 100)</h2>
            <br>
            {{ form.num(size='3', autofocus=true) }}
            <br>
            {% for error in form.num.errors %}
                <span>[{{ error }}]</span>
            {% endfor %}<br>
        </p>
        <p><input type="submit" value="Submit" name="btn"></p>
        {% if wrongin %}
            <span>Whole numbers 1-100 only</span>
            <br>
        {% endif %}
        Tries: {{ tries }}
    </form>
    <br>
{% endblock %}

1 个答案:

答案 0 :(得分:1)

如果您希望继续使用render_template,可以通过以下方式添加此行:

    form.num.data = None

在致电render_template之前。这将删除该特定字段的表单中的数据。

对于实际应用程序,最佳做法是在表单提交后立即重定向,以便用户无法单击浏览器上的“刷新”按钮并意外重新提交表单。如果您需要在重定向之间传递数据,您可以查看Flash消息,甚至使用Session来存储数据。