To make web forms, we need to use Flask-WTF.
- [Instructor] What have we done so far? We created a users table in the database and wrote a user model that we can use to read and write to this table. In this section, we'll create a signup page, so that we can take in user data and save it to the database. In this particular video, we'll install Flask-WTF. Here's how everything will fit together. First, a user will visit the URL sign up to create a new account. The page will be retrieved through an HTTP GET request and will load in the browser. Second, the sign up page will have fields for a user's name, email, and password.
The user will fill out the form and press Sign up. Third, when a user presses Sign up, the forms will submit to the flask app through a post request and will hit routes.py. Fourth, in routes.py, a function will check whether the submitted data is valid. This means checking whether all fields are filled out, whether the email looks like a real email address, and whether the password is longer than six characters. Fifth, if any of the submitted data does not pass validation, the sign up page will load again, with helpful error messages, so that the user can try again.
Otherwise, if all fields are valid, we'll save the user's credentials to the database. The user will be signed in and redirected to the homepage. We're going to use the sign up form to take a new user's data and save it to the database. Now, how do we actually write this form? Remember in the previous section, we saw that instead of packing in extra functionality, Flask lets you add it on as needed, using extensions. An extension is a package that adds a specific functionality to a Flask app. To add functionality for web forms, we'll use Flask-WTF.
Flask-WTF lets us define forms in Python, so we don't need mess around too much with the HTML. To install Flask-WTF, we'll use the same sequence of commands as we did when adding Flask-SQLAlchemy. First, get back into the apps. Isolate the Python development environment. Next, use pip to install Flask-WTF. Now that Flask-WTF is installed, let's use it to create a form in the next video.
The need for talented developers is greater than ever before. A basic grounding in a framework as simple, powerful, and easy-to-learn as Flask—a Python micro framework—can help launch your programming career. Get a basic grounding in the fundamental concepts of web development, as well as the hands-on experience required to successfully build web apps with Flask, in this course with Packt trainer Lalith Polepeddi.
It starts with turning your computer into a modern development machine. Then learn everything you need to know about Flask: models, views, controllers, web templates, forms, validation, and API interactions. Lalith shows how to apply these concepts and develop a powerful database-backed Flask app, including a full authentication system. Finally, Lalith shows how to extend the app's functionality by integrating it with third-party APIs. When you finish this course, you will be fully equipped to build your very own custom web apps with Flash.
- Setting up your development environment
- Creating a homepage
- Building templates
- Routing with Flask
- Deploying to Heroku
- Storing data
- Creating and rendering forms
- Validating form data
- Creating a user authentication system for login and logout
- Implementing location-based app features