Creating a "users" table in the "learningflask" database.
- View Offline
- [Instructor] In this video, we'll create a table inside the database that stores user information. So far we built a flask app containing two static pages. In doing so, we started developing a generalizable work flow that we can use to build out the rest of the app. This work flow is summarized by the request-response cycle. When you type in a URL and press Enter, your request hits routes.py. In routes.py, the URL is mapped to a python function that fetches web template and its assets and renders it to HTML.
That rendered HTML is sent back to the browser, where it displays to the user. The problem with this app so far is that it doesn't do anything interesting. The home page and the about page look the same for all users. Their content is static. How can we create webpages that are personalized to each user? How can we let users sign up for the app so that they can do things like view places around them? We'll talk about that in this section. In this video, we'll create a table inside the database that stores user information. To do this, we need a place to safely store a user's data.
We can do this with a database. Here's how a database fits into their request-response cycle. When a user types in an address and presses Enter, the browser issues a request for that URL. The request hits routes.py, where the URL is matched to a python function. The python function can interact with a database. It can write information to the database, like creating a new user. It can read information from that database, like places near a user.
Or... it can read information from the database, like places near a user. Python function can pass along data from the database to a web template, render it to HTML, and send it back to the browser so that the user can see it. By adding a database, we move beyond static websites and create dynamic websites, where we can safely store user data and render content and pages that is customized to each user. Let's begin by creating a system that lets users sign up for this app. To accomplish this, we need a place to safely store user data.
We can do this by setting up a database. There are four things we need to do. First, install a database engine. Second, create a new database called "learningflask." Third, create a new table called "users." And fourth, add a new user. We've already completed steps one and two in the first section. We installed Postgres and created a database called "learningflask." So we're good to go on those two. Let's move on to the third step. Inside the learningflask database, we need a table where we can safely store users' information.
We'll call this table "users." The information that we'll want to store are a user's first name, last name, email, and password. Quick note on passwords. Unlike first name, last name, and email, it is not a good idea to simply store passwords as is in plain text in the database. The reason for this is that if someone breaks into your database, they would be able to see all your users' passwords. Not good. One way to defend against this is to encrypt passwords first and then store that encrypted password into the database.
The pwdhash column is short for password hash. We'll take passwords and encrypt them, using a hash function, and then store them in the database. Flask provides hash functions that we'll use to make this pretty straight forward. Start your Postgres server. Depending on the installation you used, this startup step will be slightly different across different installations. I downloaded Postgres app, so I'm going to open this app and open psql.
And this opens up a new terminal window for me called psql, where I can interact with the database. Let's enter the learningflask database. In the Postgres we do \c and the name of the database. Now I'm connected to the learningflask database. Create a new users table, using the CREATE TABLE statement. The columns to create are uid for the user ID. And then first name, last name, email, and pwdhash.
For first name, last name, email, and pwdhash, we're using the VARCHAR datatype. This lets us store strings of variable length. And then for the UID column, we're using the serial datatype. This will let us generate an ID for each row that automatically increments. More on this in a bit. Using a select statement from that the table was created. And you can see that this table is empty. There are no rows in this table just yet. Now that we've created a new users table, let's move on to the fourth step and add a new test user to this table.
Let's use an INSERT INTO statement, to create a new user. We'll want to store encrypted passwords into this table. So this user entry is temporary, just for testing. Now when we run a SELECT statement, we see the table has one row, has one user, and the uid field has auto-incremented. So far, we've created a users table in the database. Next, let's connect this database to the flask app.
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