Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Since users can post updates, we should make views to handle showing the updates.
Pagination docs
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
I want to make our site kind of like
Twitter was in the early days.
0:00
You could read all of the messages in the
system on the front page when you weren't
0:03
logged in, and of course, you saw just a
single user's messages on their page.
0:06
So, let's copy that.
0:11
Okay, so let's build our homepage.
0:12
So let's come back over here, and we have
our index route that we've so
0:16
far done really nothing with.
0:19
All right.
0:21
So, on the index page, we need to get the
stream.
0:22
We need to get stuff.
0:25
So let's say, stream, our stream of posts
is models.Post.select,
0:26
and I'm gonna set a limit of a 100.
0:31
If we, say this was insanely popular and
0:34
we had 5 million posts, we wouldn't want
to select all of them.
0:37
So we're just gonna do a 100.
0:41
Peewee does have stuff built in for
pagination, so you can have page one,
0:43
page two, page three.
0:46
I'll leave that to you to look up, but
it's right there in the doc.
0:47
So then we want to return render_template
stream.html, stream equals stream.
0:51
Cuz that's what we're gonna run.
0:59
We're gonna return that stream.
1:00
And let's go ahead and save that.
1:02
Let's build this stream.html.
1:04
I think that will be fairly
straightforward.
1:06
We need to get a new template.
1:08
[BLANK_AUDIO]
1:09
stream.html.
1:11
[BLANK_AUDIO]
1:13
Okay.
1:15
So now, in stream.html,
1:16
let's do extends layout.html.
1:20
And let's do block content.
1:25
End block.
1:31
And then we'll do for post in stream.
1:34
[BLANK_AUDIO]
1:37
End for, okay.
1:43
So now we have to render each of our
hosts.
1:46
And I kind of wonder what we want that to
look like.
1:50
So, okay, I think you've got a pretty good
idea what we wanna have in here.
1:54
We wanna make an article tag, cuz each of
these is an article.
1:58
And let's do an h2.
2:03
Inside that h2, let's do an a href.
2:06
And we'll do URL for stream username
2:11
equals post.user.username.
2:15
In just a moment, we're gonna build a
thing where we can go to a user's page.
2:21
And we wanna do post.user.username.
2:24
Okay, so that's our h2.
2:30
We're gonna add a thing here that has a
clock,
2:32
just a little icon, and then we're gonna
add a time element.
2:35
And this is mainly for a nice little
JavaScript thing, post.timestamp.
2:42
And then we're gonna give this a class of
2:49
distime to make sure our JavaScript can
find it.
2:54
And then, to be valid HTML5, we're going
to put in our date time.
2:59
We're gonna do post.timestamp.
3:03
And then we wanna call strftime on this.
3:06
And we wanna do year, month, day,
3:10
a space, hour, minute, second, and
3:15
yes, as always, I did have to look that
up.
3:20
And inside here, we want to render out
post.timestamp.
3:27
If you wanna make this look a little
nicer,
3:30
then you probably wanna go ahead and copy
this strftime thing as well.
3:33
I didn't originally have this in my plan.
3:38
But now that I think about it, it's
probably a nicer thing to do.
3:40
So that way, we print out the nicer
looking time.
3:44
And then I want to add one more thing,
which we're just gonna leave as blank for
3:47
right now.
3:52
I wanna make a way for us to go see a
single post.
3:53
So, what we're gonna do is we're gonna add
in a thing here,
3:56
we'll give it a class equals view and
we'll just say view, 'kay.
4:01
We're not gonna put the URL in there yet,
cuz we haven't built that.
4:06
And then we're gonna do a div class equals
post.
4:09
This is where the actual post content is.
4:13
And we say post.content.
4:16
Now, one of the nice things is that Flask,
or
4:18
Jinja2 rather, won't render any malicious
HTML in here.
4:21
So they put in HTML or whatever, it just
shows the tags,
4:25
cuz we haven't marked it as being safe.
4:28
'Kay.
4:31
So, we have this thing here, this URL for
stream.
4:32
So before we go look at this, we need to
actually have the stream view.
4:35
So, let's go ahead and build that really
quick.
4:41
Let's go back to app.py.
4:43
We have index, app.route, and we'll say
stream, and
4:47
then you remember that when we built it,
we said it might have a username.
4:52
Oh, man, usernames.
4:58
So we wanna make two routes, one for
stream, one for username.
5:01
And we're actually gonna make this one
kinda smart, I think.
5:03
We're gonna make it to where it can render
a couple of different ways.
5:06
Okay, so by default, the template is going
to be stream.html,
5:12
just like the one above.
5:17
Because, in my mind, if you go to /stream,
then you get your stream.
5:18
You get the posts that you and the people
you follow have posted.
5:23
But if you go to stream/ and then a user
name, you get that user's posts.
5:27
Okay, so, if there's a username and
5:32
the username does not equal the current
user's username,
5:36
then we want to do user equals
models.User.select().where
5:43
models.User.username is like username.
5:50
So what this does here, right there,
5:55
is, that does a comparison without caring
about case.
5:57
So if my user name is lower case
K-E-N-N-E-T-H-L-O-V-E,
6:01
and you type in capital
K-E-N-N-E-T-H-L-O-V-E, it'll still match.
6:06
So we'll do .get, and we'll say stream
EQUALS user.posts.limit 100.
6:12
So, that user's posts.
6:19
Else, otherwise, we've gotten a username
and we've gotten our own stuff, right?
6:24
So it's our username, or, we left off the
username, and
6:29
it's just, we're coming to our own page.
6:33
stream equals current_user.get_stream,
6:35
and limit is gonna be 100.
6:42
And user equals current_user cuz we may
need a user while we're in there.
6:46
All right?
6:53
Now, regardless of what happened up there,
if we got a username,
6:56
then we wanna set template equal to
user_stream.html.
7:03
And we're gonna return render_template.
7:08
We're gonna render whatever template we
picked.
7:14
Our stream is gonna be the stream, and our
user is gonna be our user.
7:16
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up