Python Forum
making a form purely with flask-wtf
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
making a form purely with flask-wtf
#5
Should not add so many 3-party libraries before you understand the basic of Flask.
You haven't defined a route to handle the form rendering and submission in your Flask application.
Not gone explain this now,but here are working code you can try.
test.py:
from flask import Flask, render_template, redirect, url_for
from flask_bootstrap import Bootstrap5
from flask_wtf import FlaskForm, CSRFProtect
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired, Length

app = Flask(__name__)

app.secret_key = 'tO$&!|0wkamvVia0?n$NqIRVWOG'
bootstrap = Bootstrap5(app)
csrf = CSRFProtect(app)

class NameForm(FlaskForm):
    name = StringField('Which actor is your favorite?', validators=[DataRequired(), Length(10, 40)])
    submit = SubmitField('Submit')

# Define a route for the form page
@app.route("/", methods=["GET", "POST"])
def index():
    form = NameForm()
    if form.validate_on_submit():
        name = form.name.data
        return redirect(url_for('thanks', name=name))
    return render_template("index.html", form=form)

# Define a route for the thank you page
@app.route("/thanks/<name>")
def thanks(name):
    return f"<p>Thank you, {name}!</p>"
index.html:
<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Favorite Actor</title>
    {{ bootstrap.load_css() }} {{ bootstrap.load_js() }}
  </head>
  <body>
    <div class="container">
      <h1>Favorite Actor Form</h1>
      <form method="POST">
        {{ form.hidden_tag() }}
        <div class="form-group">
          {{ form.name.label(class="form-control-label") }} {{
          form.name(class="form-control") }}
        </div>
        <div class="form-group">{{ form.submit(class="btn btn-primary") }}</div>
      </form>
    </div>
  </body>
</html>
Reply


Messages In This Thread
RE: making a form purely with flask-wtf - by snippsat - May-29-2024, 08:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Beta6 of my password flask-wtf web form robertkwild 3 135 Yesterday, 10:30 AM
Last Post: robertkwild
  Show powershell errors in flask-wtf web form robertkwild 14 467 Jun-30-2024, 03:15 PM
Last Post: robertkwild
  Running powershell command in flask wtf form robertkwild 10 381 Jun-27-2024, 09:49 AM
Last Post: robertkwild
  using split in my flask wtf form robertkwild 1 202 Jun-11-2024, 05:19 PM
Last Post: deanhystad
  error while inserting values into a table from form in flask in postgreSQL sahilsiddharth 3 7,354 Jun-05-2017, 07:49 PM
Last Post: sahilsiddharth

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020