#100DaysOfWeb in Python Transcripts
Chapter: Days 57-60: Flask Login
Lecture: Make the create-user Jinja Template

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now let's create some users. In order to do this, we will create a template. So, hop in to your templates folder
0:09 and all we should have in there is index.html. This time, we're going to create one called create_user.html.
0:20 Now, I'm just going to do a little bit of magic because I'm sure you don't want to watch me type all of these out
0:25 so let's just get the code in, the HTML code and we'll go through it. And that's the template. So, in this, let's walk through it.
0:34 We have a couple of things here to note. First, there's your title at the top, Create User Page. That's nice and simple, self-explanatory.
0:42 We then have a header that's going to appear on the page that says, this is a page where we can create a new user.
0:49 Now, for the meat, here is a div that has a form and this form is using the POST method as we've discussed before
0:58 to push data back to our web app backend. And the action upon posting so after it's finished posting the data
1:08 we're just going to redirect the user to the same page. They're going to stay right here. Okay. Label is going to be user creation.
1:17 The label for this table, create your user. And the input, we're going to have an input field and as you can guess, it's going to be called username.
1:28 And the placeholder text that will appear in that box is The Real Bob. And then we have a second input field for our password placeholder, Not ABC123.
1:40 And finally, it ends with a Submission button. Now, the special thing about this are these names. This is what we are going to reference
1:52 in the backend of our Flask app as we have done before in the previous apps of this course and we're going to head over to that file now.
2:03 Let's save that, head back, vim route.py and there is our basic, basic app. First things first, we are using the render_template
2:16 so we have to make sure that's imported which we do have already. And then we need to actually import request. Now, why are we importing that?
2:27 That is to allow us to import the data from our template. So, request. Now, let's create the actual route. We'll add an extra space there.
2:42 I know, technically, we don't have to, but let's just do it. This is going to be an app route. So first, let's surround the decorator, app.route
2:53 and it's going to be called create user. And the methods, if we remember from our previous videos are going to be GET and POST
3:08 because we're able to interact with this page both ways. And then we close it off. All right. And let's create the user. So, def create_user.
3:23 If request.method, so this is very similar to all of our other apps, so let's make this quick.
3:29 In fact, let's skip forward to me not typing all of this. And there it is, you didn't have to watch me do it but let me explain it quickly.
3:37 So, similar to our apps before if the request method is a POST method and username is in that request form that's being posted back to us
3:50 then we're going to collect that data. So we're defining a variable here or an object here called username
3:56 and it's going to be assigned the value of username that was entered by the person into our form that we defined in the previous template.
4:05 Same with password. We're collecting that data, the username and the password based on those two name labels that we gave those fields.
4:16 And then we're actually going to call a new function which I have yet to write and it's going to add a user to our database
4:25 using both of those objects, username and password. And then we close off our function here with return render_template create user.
4:34 So remember, this stuff here is only going to work it's only going to be called and used if there is a POST method. For loading the page
4:45 you're just going to simply call on this render_template and load the create_user.html page that we saw before, and that's that.
4:54 So now, let's go down and create ourselves a function to add a user to the DB.


Talk Python's Mastodon Michael Kennedy's Mastodon