0Pricing
Flask Academy · Aula

Renderize e envie um formulário

Mostre campos em um modelo e processe o envio.

Renderize e envie um formulário é uma aula grátis de Flask Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Flask Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Flask Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em inglês.

From Class to Page

A form class is useless until a user can see it. Now you will render those fields into HTML and accept the submission back. 🖋️

Pass the Form to the Template

Create the form in your view and hand it to render_template. The template gets a live object full of fields it can draw.

form = LoginForm()
return render_template('login.html', form=form)

Open the Form Tag

In Jinja, start with an HTML form tag set to method post. That tells the browser to send field values in the request body.

<form method="post">
  ...
</form>

Render the Hidden Tag

Call form.hidden_tag() first inside the form. It drops in the CSRF token your post will need to be accepted.

<form method="post">
  {{ form.hidden_tag() }}
</form>

Draw a Field

Each field renders itself when you call it. Use field.label for the text and the field itself for the input element.

{{ form.username.label }}
{{ form.username() }}

Pass HTML Attributes

Calling a field accepts keyword arguments that become HTML attributes. Add a class or placeholder right from the template.

{{ form.username(class="input", placeholder="Your name") }}

Render the Submit Button

The submit field draws the button. Calling form.submit() produces the input that posts the whole form.

{{ form.submit() }}

Allow POST in the Route

By default a route only answers GET. List methods so the same view can serve the page and receive the post.

@app.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()

Read Submitted Data

After a post, each field exposes a .data attribute holding the typed value, already coerced to the right Python type.

username = form.username.data
password = form.password.data

Auto-Populate on Reload

Flask-WTF fills fields from the request automatically. If you re-render after a post, the user's input stays in place.

form = LoginForm()  # picks up posted data on POST

One Template, Both Verbs

A single view handles GET and POST: build the form, process it on post, and re-render the same page otherwise.

form = LoginForm()
if form.is_submitted():
    pass  # handle data
return render_template('login.html', form=form)

Quick Check

What must you render inside the form to include the CSRF token?

Recap

You rendered fields with labels and attributes, added hidden_tag for CSRF, allowed POST, and read values from each field's .data. 🎉

Perguntas Frequentes

A aula “Renderize e envie um formulário” é grátis?

Sim — o texto completo de “Renderize e envie um formulário” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Flask Academy, atualize para CoddyKit PRO. O curso de Flask Academy inclui 4 aulas no total.

O que vou aprender em “Renderize e envie um formulário”?

Mostre campos em um modelo e processe o envio. Você pratica Flask Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Flask Academy?

Nenhuma experiência prévia é necessária. Flask Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.

Quanto tempo leva a aula “Renderize e envie um formulário”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Flask Academy?

Sim. Cada aula de Flask Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Defina uma classe FlaskForm
  2. Renderize e envie um formulário
  3. validate_on_submit e tokens CSRF
  4. Validadores personalizados e erros de campo
← Voltar para Flask Academy