Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Redirects and the admin section
Lecture: Add redirect view methods

Login or purchase this course to watch this video and the rest of the course contents.
0:00 It's time to add redirects dynamically as an external user, not just typing in the source code of our website.
0:08 That's how, of course we want people to work with it And we're gonna use one of two of these URLs here.
0:13 In the beginning, we used add to add new ones, but eventually you want to edit them. So we're either gonna go to /admin/add-redirect to add a new one,
0:21 or we're going to go to /admin/edit-redirect and then give the ID to edit that. Those obviously don't exist yet over here. So we're gonna write them.
0:32 Now, these are going to require a slightly different styles. So I'm going to put a little divider here. We'll call this add redirect.
0:42 Why such a divider? Because we're actually gonna have two functions. They're both gonna kind of look like this. So we'll clone that.
0:50 First thing, let's go and say add redirect. And then the route is add redirect.
0:57 And what are we gonna put here? It turns out that adding and editing are going to be identical. So let's just say this is going to an edit redirect.
1:07 But we want a different url so we're not gonna change this or this.
1:10 But it turns out the same form works for both, so we don't have to write it twice. That's cool. Now, to follow the get, post, redirect pattern,
1:19 sometimes you'll see people say, if request.method equal do get such and such
1:28 else do such and such. You should never do that right. That makes it super complicated. Don't do that.
1:33 What we're gonna do instead, as we're gonna go over here and use the web infrastructure. Say method equals get like that.
1:40 And this is going to let us hook into that. And this function will only get called if it's a GET.
1:46 So because of that, we need to have different names of more than one function. We already ran into that error.
1:52 We're going to have one like this, and then we're gonna have one for POST. And this is gonna be called post too.
1:59 This one handles showing the form. This one handles processing the user data submitted when they save the form.
2:06 Oh, and I forgot, this is request method, not just method.


Talk Python's Mastodon Michael Kennedy's Mastodon