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