Skip to content

Instantly share code, notes, and snippets.

@iuriguilherme
Created October 27, 2023 01:20
Show Gist options
  • Save iuriguilherme/5ba5f13c19717a3a28fc405f6bc4ea90 to your computer and use it in GitHub Desktop.
Save iuriguilherme/5ba5f13c19717a3a28fc405f6bc4ea90 to your computer and use it in GitHub Desktop.
Multiple forms Quart WTF
@app.route("/test", methods = ['GET', 'POST'])
async def test(*args, **kwargs) -> str:
"""Tests"""
try:
if 'error' in kwargs:
raise Exception(kwargs.get('error'))
print(f"request.form: {await request.form}")
print(f"forms: {[f for f in (await request.form)]}")
print("form-zero" in (await request.form))
user_login_form: QuartForm = UserLoginForm(
formdata = await request.form)
user_register_form: QuartForm = UserRegisterForm(
formdata = await request.form)
if request.method == "POST":
print(f"""Formulário de login de usuário: \
{user_login_form} ({type(user_login_form)})""")
print(f"""Formulário de registro de usuário: \
{user_register_form} ({type(user_register_form)})""")
return await render_template(
"index.html",
user_login_form = user_login_form,
user_register_form = user_register_form,
)
except TemplateNotFound:
print("Template não encontrada")
await abort(404)
except Exception as e:
logger.exception(e)
return await render_template(
"error.html",
error = repr(e),
title = "Erro",
user = user,
**metadata,
)
{% if user_login_form %}
<h3>UserLoginForm</h3>
<form action="" type="submit" id="login-form" name="login-form" value="login-form" method="post">
{% for field in user_login_form|selectattr('render') %}
<div class="form-floating">
{{ field.label(class="form-label") }}
{{ field(class="form-control") }}
{% for error in field.errors %}
<span class="badge badge-danger">[{{ error }}]</span>
{% endfor %}
</div>
{% endfor %}
{{ user_login_form.submit_field(class="btn btn-primary") }}
</form>
<hr>
{% endif %}
{% if user_register_form %}
<h3>UserRegisterForm</h3>
<form action="" type="submit" id="register-form" name="register-form" value="register-form" method="post">
{% for field in user_register_form|selectattr('render') %}
<div class="mb-3">
{{ field.label }}
{{ field }}
{% for error in field.errors %}
<span class="badge badge-danger">[{{ error }}]</span>
{% endfor %}
</div>
{% endfor %}
{{ user_register_form.submit_field(class="btn btn-primary") }}
</form>
<hr>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment