Rendering Flask-WTF forms in templates
In this lesson, we will learn how to render the forms on the front-end.
To render the form, we first need to return it from a view to the template.
Modify login
view in app.py
Now let’s modify the application module to use the forms.
Import LoginForm
Let’s first import the LoginForm
from the forms module forms.py
.
from forms import LoginForm
Create an object of LoginForm
in the login
view
We will then create an object of this form inside the login
route.
@app.route("/login", methods=["GET", "POST"])
def login():
form = LoginForm()
Return the form to the template
This form will be passed to the render_template
function as a named argument.
return render_template("login.html", form = form)
Complete implementation of app.py
The complete implementation of the modified app.py
is given below:
Get hands-on with 1400+ tech skills courses.