May-30-2024, 08:54 AM
(May-29-2024, 08:33 PM)snippsat Wrote: 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>
sorry but why have you created a two documents a "test.py" and a "index.html" cant you have it all in one document