1 00:00:00,680 --> 00:00:03,820 As you can probably guess, there won't be a lot to our stream form. 2 00:00:03,820 --> 00:00:07,200 But we need to create a view and we should probably make the form available all 3 00:00:07,200 --> 00:00:12,065 the time so we can let the user post a new message no matter where they are. 4 00:00:12,065 --> 00:00:14,370 Okay, so if we're going to make a form for 5 00:00:14,370 --> 00:00:19,170 them to post things from, then we to go back to forms.py. 6 00:00:19,170 --> 00:00:21,910 So our forms are getting easier and easier. 7 00:00:21,910 --> 00:00:24,260 Our log in form was two fields. 8 00:00:24,260 --> 00:00:26,780 And this form is only going to be one. 9 00:00:26,780 --> 00:00:30,330 So let's make a PostForm which is a Form. 10 00:00:30,330 --> 00:00:31,540 And it's just going to have content. 11 00:00:31,540 --> 00:00:35,080 And that's going to be a TextAreaField. 12 00:00:35,080 --> 00:00:38,770 And we're gonna put in a message, I'm gonna say what's up. 13 00:00:38,770 --> 00:00:40,040 You can, of course, put in whatever you want. 14 00:00:41,280 --> 00:00:48,470 And, our only validator on this is going to be DataRequired, and that's it. 15 00:00:50,120 --> 00:00:53,605 So let me come up here and import the TextAreaField. 16 00:00:53,605 --> 00:01:00,197 [NOISE] And, we want to save. 17 00:01:00,197 --> 00:01:02,190 And there's our new Form. 18 00:01:02,190 --> 00:01:03,710 So that's great. 19 00:01:03,710 --> 00:01:06,130 So now we need to go use that form. 20 00:01:06,130 --> 00:01:11,675 So let's go back over here to app.py, and let's make a new route. 21 00:01:11,675 --> 00:01:16,114 [BLANK_AUDIO] 22 00:01:16,114 --> 00:01:18,861 And let's say a new_post. 23 00:01:18,861 --> 00:01:21,652 [BLANK_AUDIO] 24 00:01:21,652 --> 00:01:23,470 And what we could do. 25 00:01:23,470 --> 00:01:24,723 Let's think about this for just a moment. 26 00:01:24,723 --> 00:01:27,560 What we could do is we could make it to where we have one route 27 00:01:27,560 --> 00:01:31,820 that always gets posted to, that will create the new post, and 28 00:01:31,820 --> 00:01:34,330 then send you back to, like, your stream page, or whatever. 29 00:01:34,330 --> 00:01:38,110 That would make it to where if we wanted to show the form on every single page, 30 00:01:38,110 --> 00:01:39,690 then that's a really easy way. 31 00:01:39,690 --> 00:01:42,900 We can post to that and it just gets redirected. 32 00:01:42,900 --> 00:01:47,200 I think showing the form on every single page might get a little visually messy and 33 00:01:47,200 --> 00:01:52,280 we have to create and pass this post form every single time. 34 00:01:52,280 --> 00:01:56,010 That doesn't seem like the most fun thing to do, so 35 00:01:56,010 --> 00:01:58,620 let's instead just use this route. 36 00:01:58,620 --> 00:02:01,550 And we'll have a view where they can just go to post. 37 00:02:01,550 --> 00:02:05,790 And then, if you wanted to in your own version of this, you could load that view 38 00:02:05,790 --> 00:02:10,560 through Ajax, and hit a button, you pull up a model that shows that view or 39 00:02:10,560 --> 00:02:13,120 whatever, they type in their post and then it does whatever it does. 40 00:02:13,120 --> 00:02:15,870 All right, so, let's call this post. 41 00:02:17,270 --> 00:02:17,850 And you know what? 42 00:02:17,850 --> 00:02:20,506 I just realized we need to make this log in required, 43 00:02:20,506 --> 00:02:23,820 cuz we don't want non-logged in users to be posting. 44 00:02:25,230 --> 00:02:29,920 So, form equals forms.PostForm, 45 00:02:29,920 --> 00:02:34,942 and if form.validate_on_submit. 46 00:02:34,942 --> 00:02:36,030 So you notice when we're working with forms, 47 00:02:36,030 --> 00:02:38,230 we're basically doing the same thing every time. 48 00:02:38,230 --> 00:02:40,820 We build the form, and we're gonna send it to our template. 49 00:02:40,820 --> 00:02:44,300 But, if the form comes in, and it's been submitted, then we're gonna validate it, 50 00:02:44,300 --> 00:02:46,570 and see if it works, and then, do whatever. 51 00:02:48,330 --> 00:02:50,170 So, models.Post.create. 52 00:02:51,620 --> 00:02:54,740 And user is going to be g.user. 53 00:02:54,740 --> 00:02:55,980 And then, this is a fun thing. 54 00:02:55,980 --> 00:02:57,499 It actually took me a little while to find this. 55 00:02:58,510 --> 00:03:01,640 So g.user is just this kinda lazy object. 56 00:03:01,640 --> 00:03:03,560 We have to actually go and get the object. 57 00:03:03,560 --> 00:03:08,820 So we have to run this method of _get_current_object(). 58 00:03:08,820 --> 00:03:11,270 It's a little weird, so I made sure [LAUGH] and 59 00:03:11,270 --> 00:03:13,120 found out how to do this for us. 60 00:03:13,120 --> 00:03:13,950 So that's a little strange, 61 00:03:13,950 --> 00:03:18,060 but that's what we do, g.user, that's our global user. 62 00:03:18,060 --> 00:03:19,180 We need to add that in. 63 00:03:19,180 --> 00:03:20,830 Let's add that in in just a moment. 64 00:03:20,830 --> 00:03:23,930 And then we have to go and get that current object each time. 65 00:03:23,930 --> 00:03:27,000 And then content equals form.content.data. 66 00:03:27,000 --> 00:03:29,130 And let's call strip on that, 67 00:03:29,130 --> 00:03:31,373 just in case they put in a bunch of white space or whatever. 68 00:03:32,820 --> 00:03:38,510 So we're gonna flash Message posted: Thanks! 69 00:03:38,510 --> 00:03:40,490 And that's going to be a success. 70 00:03:42,320 --> 00:03:49,100 And we're gonna return a redirect url_for index. 71 00:03:51,580 --> 00:04:00,182 And then out here we're gonna return a render_template('post.html', form=form). 72 00:04:00,182 --> 00:04:03,740 Okay, so I said we were gonna do this g.user. 73 00:04:03,740 --> 00:04:08,520 So let's come up here to our before_request. 74 00:04:08,520 --> 00:04:10,530 And that's where we want to add that in. 75 00:04:11,570 --> 00:04:15,840 So what we want to do is actually I need to add two things. 76 00:04:15,840 --> 00:04:19,190 So we're going to say g.user = current_user. 77 00:04:20,420 --> 00:04:25,350 And then we're going to come up here to our login imports and 78 00:04:25,350 --> 00:04:28,130 we're going to import current_user. 79 00:04:28,130 --> 00:04:31,500 So this is just a little method or 80 00:04:31,500 --> 00:04:35,440 object that we can use that will find the current_user. 81 00:04:35,440 --> 00:04:36,460 So pretty handy. 82 00:04:36,460 --> 00:04:39,510 Let's just go ahead and look at our post form again. 83 00:04:39,510 --> 00:04:43,360 So that's that, nothing really exceptional. 84 00:04:43,360 --> 00:04:46,930 So anyway, we're rendering out this post.html, we should go build that. 85 00:04:47,940 --> 00:04:51,741 So, we'll say new file, post.html. 86 00:04:51,741 --> 00:04:59,407 And let's say that this is going to extend layout.html and 87 00:04:59,407 --> 00:05:04,160 we're gonna bring in that macro from 88 00:05:04,160 --> 00:05:09,240 'macros.html' import render_field. 89 00:05:09,240 --> 00:05:14,613 And in block content, we want to 90 00:05:14,613 --> 00:05:21,777 do form method = POST action = this page, 91 00:05:21,777 --> 00:05:25,758 form.hidden_tag for 92 00:05:25,758 --> 00:05:31,935 field in form render_field(field). 93 00:05:31,935 --> 00:05:37,416 [SOUND] And then button 94 00:05:37,416 --> 00:05:45,949 type = submit id = submit Post. 95 00:05:45,949 --> 00:05:48,580 And then that ends our form and we end our block. 96 00:05:50,965 --> 00:05:55,930 'Kay, so, if we were to go over here to TwoCans, and 97 00:05:55,930 --> 00:05:58,420 we were to go to new post, then there's our form. 98 00:05:58,420 --> 00:06:00,860 So, let's add in a way that we can get to here from where ever. 99 00:06:00,860 --> 00:06:05,092 So, we can get to the home page a couple of ways, we can log out, but 100 00:06:05,092 --> 00:06:08,100 we don't have any way to get to this post page. 101 00:06:08,100 --> 00:06:09,600 So let's go add that. 102 00:06:09,600 --> 00:06:15,590 Let's go over here to our layout.html. 103 00:06:15,590 --> 00:06:22,760 And here where we have this All link, let's add in a new thing, 104 00:06:22,760 --> 00:06:29,650 if current_user.is_authenticated, cuz we only wanna show this if they're logged in. 105 00:06:31,400 --> 00:06:33,990 There's no reason to show them a link they can't use, right? 106 00:06:35,130 --> 00:06:38,710 The URL for post, that's the name of our view. 107 00:06:39,940 --> 00:06:42,060 And class = new. 108 00:06:42,060 --> 00:06:46,170 And let's say Create New Post. 109 00:06:46,170 --> 00:06:50,300 All right, now let's refresh this. 110 00:06:53,520 --> 00:06:55,370 Sweet, we've got this Create New Post thing. 111 00:06:55,370 --> 00:06:58,670 And it's highlighted here because of some JavaScript that's running. 112 00:06:58,670 --> 00:07:01,280 Since this course is about Flask, we're not gonna talk about the JavaScript, 113 00:07:01,280 --> 00:07:03,610 but feel free to check it out inside the Static folder. 114 00:07:05,260 --> 00:07:08,450 That G object is becoming more useful than I'd expect. 115 00:07:08,450 --> 00:07:11,640 Now that we can post messages, let's show them on the index page and 116 00:07:11,640 --> 00:07:14,047 show just your messages on your personal Stream View.