Flaask-form使用

2146 ワード

baseページはもう書きません。参照してください。http://www.pythondoc.com/flask-mega-tutorial/index.html
  :login.html
{% block body %}
    

Sign In

{# form login.html form #}
{{ form.hidden_tag() }}

{{ form.remember_me }} Remember Me

{% endblock %}
この点は簡単です。formの中に提出ボタンがあります。クリックしてからページをジャンプします。
@app.route('/login', methods=['GET', 'POST'])
def logintoform():
    myform = LoginForm()
    if myform.validate_on_submit():
        return redirect('/index')
    return render_template('login.html', form=myform)
シングルで書いたのはコードをクリックした時にボタンを押してジャンプができなかったからです。原因はという文が書かれていないので、プログラミングの面ではまだ初心者で、多くの時間をかけて問題点を見つけます。
この行のコードについて説明します。
hidden_tag():

Render the form's hidden fields in one call.

A field is considered hidden if it uses the [`HiddenInput`] widget.

If `fields` are given, only render the given fields that are hidden. If a string is passed, render the field with that name if it exists.

Changed in version 0.13: No longer wraps inputs in hidden div. This is valid HTML 5.

Changed in version 0.13: Skip passed fields that aren't hidden. Skip passed names that don't exist.

validate_on_submit():
Call validate() only if the form is submitted. This is a shortcut for form.is_submitted() and form.validate().
よく読んでいません。またFlashk-wtf文書を見ます。
If your form has multile hidden fields,you can render them in one block using {{ form.hidden_tag() }}.
{{ form.hidden_tag() }} {{ form.name.label }} {{ form.name(size=20) }}
Validating Forms
Validating the request in your view handles:
@app.route('/submit', methods=('GET', 'POST'))
def submit():
    form = MyForm()
    if form.validate_on_submit():
        return redirect('/success')
    return render_template('submit.html', form=form)