Forms6:51 with Kenneth Love
Most web apps use forms for gathering data. Ours will be no different, so we need to figure out how to get to the form data easily and cleanly.
url_for(): This function, always available in templates but importable in your app, finds the URL for a given view function name.
redirect(): This function returns an HTTP redirect to whatever URL is provided.
If you're not familiar with the
pdb library that we used, check out Write Better Python.
[MUSIC] 0:00 [SOUND] To build our web app, 0:03 we need a few more pieces of Flask's provided functionality. 0:05 Also, we're gonna be using several images and some pre-built CSS. 0:09 So make sure you start with the work space associated with this video. 0:12 Something pretty much every web app uses are forms. 0:16 There are a lot ways out there for handling forms in Flask, but 0:19 we're just going to use them directly for this course. 0:22 Let's check them out. 0:24 We're gonna start a new project. 0:25 So I'm gonna go ahead and create a new app.py, and you 0:27 can see that we already have a templates directory and a static directory. 0:34 This is thanks to you know the magic of editing and 0:39 our awesome designers here at Treehouse. 0:43 so don't worry about what's in there we're gonna modify some of 0:45 what's inside templates but not what's inside static. 0:49 And the options are something I set up for us. 0:52 already so we don't have to build it. 0:54 while we're building the app. 0:56 It basically just. 0:57 Kind of defines what our options are, hence it being called options. 0:59 So let's go ahead and create our Flask app which means to import flask, and 1:04 we need to make our app, and we need to run our app. 1:11 And again we have to specify our host, and our port. 1:18 All right, so let's go ahead and actually get this running. 1:25 Python app.py and then I'm gonna open that up. 1:29 [BLANK_AUDIO] 1:36 There we go, we've got that. 1:40 Off to the side, awesome. 1:43 All right. So there we go. 1:45 We get the not found, cuz there's nothing there's no routes yet. 1:47 So we should probably add an app, add a route, right? 1:51 So let's render a template. 1:55 Let's, @app.route for that home page there. 1:57 Index, return render, template index.html. 2:06 Okay, so let see what that does. 2:15 Render template is not defined. 2:21 That's because I forgot to import it. 2:22 So let's import that. 2:24 [BLANK_AUDIO] 2:25 All right. 2:28 [BLANK_AUDIO] 2:28 All right. There we go. 2:31 So we've got this cute little bear. 2:32 We gotta form. 2:37 But that form is probably not going to do anything. 2:38 Lets, lets look at our template. 2:40 And yeah that form's not going to do anything. 2:43 It's just going to post right back to itself. 2:46 So what that means is that we have to tell this where to send its information to. 2:49 What I want to do is I'm going to write. 2:57 I'm gonna write a new method or a new function rather. 2:59 Let's go over here, and let's make a new one that's called save. 3:03 And I want this to only be accessible, if you post to it. 3:11 'Kay. 3:20 So since I only want you to post to it. 3:22 What I want to do is you know right now we're gonna call this save. 3:24 Let's just have it return Saved. 3:28 All right. 3:34 And over here we're gonna add a new item. 3:37 We need to tell it. 3:41 To print out the URL for a method or for a function rather, which is called save. 3:42 So it goes and looks at save. 3:51 Figures out what the route it and goes, oh, it's save. 3:54 Okay. Cool. 3:57 So, that's where we're going to send it. 3:58 All right. 4:00 Refresh this and 4:04 we're gonna name the bear, I'm gonna name I'm gonna name the bear Mike. 4:05 Let's build it. 4:09 Yea! I hit Save. 4:12 So, that's fairly straight forward. 4:15 Let's actually make it to were we, we don't do this we just get redirected back 4:19 we need to import a new thing so from flask import redirect 4:25 and from flask import url_for. 4:32 We can actually do all these together, so 4:37 let's just do render_template, redirect, url_for. 4:38 That way we can have them all down there. 4:45 And what we want to return is a redirect. 4:48 We want it to redirect to the url_for index. 4:53 All right? 4:57 So, let's go back. 5:01 There's Mike. 5:03 Build it. 5:04 Brings us right back. 5:05 All right. 5:07 So that's pretty cool. 5:08 We handled it, it redirected back to us. 5:09 You know, let's actually let's see what's happening, 5:12 when we do the redirect, like let's see what comes in from this form. 5:16 The easiest way to do that, is to go in 5:21 here and bring our own old friend pdb. 5:25 So, we're gonna set a trace right there. 5:31 Make this a little bigger. 5:36 That should give you a hint of what's going to happen. 5:37 All right. 5:40 And you know what, just to make sure that we have the information let's 5:41 import request up there. 5:45 Okay. 5:47 So we do this. 5:48 We have Mike the bear and here, well let's see what we have. 5:49 We just have PDB. 5:55 Do we have request? 5:59 We do, there is a request, there's a whole lot of stuff here, so 6:00 luckily I know what it is that we need to look for. 6:05 What we want to look for is inside request, 6:08 there's a thing called form which is this immutable multidict. 6:11 I'm really not sure what it means by multidict, but 6:17 immutable means that we can't change it. 6:19 And so what this tells us is that we have name as a key and Mike is a value. 6:21 So that's pretty cool. 6:29 I'm gonna go ahead and let the view continue. 6:30 And then I'm gonna take PDB out of my view, 6:33 cuz I don't wanna have that just hanging around. 6:36 [SOUND] Great, we have a form and 6:37 we've seen how to read data from it. 6:42 In our next video, we're going to look at using cookies to store and retrieve data. 6:47
You need to sign up for Treehouse in order to download course files.Sign up