Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Routing for the CMS
Lecture: A better 404 error page

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, we're making really good progress with our Web app here, but notice if we go to something that doesn't exist,
0:06 We get this underwhelming 'page not found' 404 status code error.
0:12 Now it turns out having a better 404 page can be helpful and can even be a great way to catch users and help them find the thing they're looking for
0:20 Check this out. Here, check out the 11 best 404 pages, and I love these really creative ones that you might go and find.
0:30 So here. ""Error 404. I find your lack of navigation, disturbing", right.
0:35 Here's one for hootsuite that talks about how you Congar find what you're looking for. is an old school looking one. That's kind of fun
0:43 The Lego 'oh no! Something's' come unglued'. Over at Talk Python, we have one that's kind of fun.
0:50 Course it's a podcast and you're supposed to plug in your your ear buds and listen
0:55 But if there's no page, well, maybe there's just empty air. Headphones here, something like that.
1:01 So what we want to do is we want to add something fun to our site rather than this. So really, really quickly, I'm gonna show you how to do that.
1:08 Why is this relevant? Well, because over in our CMS thing, right, we're catching everything and
1:13 then for stuff that doesn't exist or saying it's not found right?
1:17 So we want to kind of build out what happens in this section to make it slightly nicer.
1:22 So I've come up with something fun that I'll go and share with you in a second. Image and whatnot I created.
1:27 So here is the image that is the logo of PyPI, right. This is their logo. So I thought I'd be fun if that kind of looked broken.
1:37 So what we got to do is we basically have to just say write a view method here, a special one that's gonna handle when there's a 404.
1:44 We're gonna go over here or something like this. This will be 'not found'.
1:48 And I'll just put it in this utilities controller and what we want is a not found view config like that. And the route name will be, let's put 404.
2:00 And that this will be, I already dropped it over here 404.pt.
2:07 There we go and for this, We're gonna go set the response, its status equals 404. Like that.
2:16 And it turns out that we're going to need to give a little extra information over here.
2:21 So we' ll say, go and get this thing called our view model or view model base. and then we'll just say, give me the dictionary. Not that one. Like, so.
2:34 the reason we have to do this is there's some information like the logged in user ID
2:39 Whether or not that set or not, that has to be communicated to the outer shell like the navigation and stuff
2:44 And it wants me to pass the request, though it can go check the request for a logged in user. Things like that
2:50 All right, well, with this little change. Run this. It doesn't love it.
2:54 Oh, yeah, the 404 route name. I don't think I need to set a route name here. It's just '404'.
3:01 Okay, cool Cool Now let's go try this again so we could still go around our site, right?
3:06 You can see it's still working, but if we hit something like 'donate', which doesn't exist now, let's refresh the CSS.
3:13 Now we get this cool page like this, "We just can't find that page, sorry".
3:17 Head back to the top of the site and then here's this logo kind of broken and spilled out on the page.
3:21 That's fun, right? So let's go and this one also was a 404 right? So now let's get this cool 404 page.
3:26 Maybe I should put the word 404 in here. I don't know. but it seems decent, because as we work with these different pieces.
3:34 We're gonna wanna have some kind of better response than just know that that blank text message.
3:40 anyway he would were able to use this cool 404 page and all we had to do was add a 'not found' view config and say, here's the HTML for it.
3:49 And because the HTML uses this paired layout which ask questions like down here, is there a user and as that user and admin those kinds of things,
3:59 it has thio have like a little bit of bass detail that comes from the base view model So instead of recreating that, we just passed it along.
4:07 Easier to make the site work that way, keeps you logged in and whatnot. Yeah, I think that's really fun, let's go play with it one more time.
4:13 Yeah. I'm sorry. We just can't find that page. Here's our cool 404 page.


Talk Python's Mastodon Michael Kennedy's Mastodon