Adding a CMS to Your Flask Web App Transcripts
Chapter: The redirects (our bit.ly)
Lecture: Add redirect view methods

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, I hope that you really love the redirects that are already built into the site because we can't add any new ones.
0:08 But click over here. We just get four or four. Guess you don't have that functionality. You can just use the ones that are hard coded.
0:15 Of course, that goes entirely against the whole purpose of the CMS to let us come here and edit the content without changing the source code.
0:23 So this is what we're gonna focus on now. We want to work on this girl slash admin slash redirect.
0:31 Now, this is this is where the view model idea and the routing and all that. It's a little bit more interesting.
0:36 So let's go over here and we're going tohave. There you go. We got this ad redirect, and let's go ahead and change this method name.
0:46 And that would be totally reasonable to put add redirect here. But it turns out that add and edit are the same thing,
0:53 and add is like a simpler version of edits. So let's just reuse the edit template that we're going to need for go back here
1:03 for this button right there. Okay, so we're gonna use an edit instead of an ad.
1:09 Now there's one other thing that we've got to focus on here that's gonna get interesting Let's divide this a bit here like this.
1:26 And notice views. Plural. Okay, so one of the things that I see some people doing it will come down here
1:32 And I will say, if flash not request the method is get we're going to do this else. LF blast not request,
1:46 that method is post. We're gonna do something else. No, no, no, please don't do this. This is not the way you want to do it.
1:56 One. This makes this method do two things. Not one thing which violates one of the core design principles of writing clean code into
2:05 it means every single thing you actually work on is just mawr indented than it should be. What you really want to do is think of how do I show them
2:12 the form? How do I process them submitting the form? Those are two different things. So luckily,
2:17 we can come over here and use the routing infrastructure very easily with commissioners say the
2:22 methods is an array off. Http Methods were willing to accept. Guess What one do we want for here?
2:30 Get That's it. And they would have another one like this. That is for Post. Now again, we saw Got to change the name here and here.
2:42 So let's change this to underscored get And this one to underscore Post. It doesn't matter. We could call this zebra.
2:51 Whatever. It doesn't matter. The thing that users see is described, right Here's slash admin slash add re direct and that's the same here and here.
3:00 But the verb the to be verb is different. So this is just for us to understand what's happening.
3:06 All right, so this this one needs some work here and some spilling. Lots of spelling, apparently. But this one is pretty close.
3:18 What we're gonna do is we're going tohave and it it redirect view model. Why not add? Well, exactly same reason is this right?
3:27 The view models job is to exchange the data and do validation and version like strings
3:33 integers from the HTML form. Because the former they're saying we should be able to use a common view model as well.
3:39 So gonna try that it doesn't work will unwind it. So it's coming out here is gonna be an edit. Redirect D. Amato, Major Active,
3:54 you model. And let's just leave it empty for the moment. If we import that, that's a good sign or, more likely to need this one here.
4:10 Okay, well, let's go ahead and give this a run and see if we've got it. Sort of working at least that we can quick the Lincoln.
4:20 It's going to say this doesn't exist, but I'll see if we get that far. All right? Can we edit it? No, no, we can't because we only wrote the ad.
4:30 Let's try adding, there we go. That looks like an error, but to me it feels like success because we got our function to run. It just says,
4:38 Well, now your job is to write the HTML, so that's the next up. But we've created this nice separation here for our get
4:46 post redirect pattern. So we have our get. Then we do our post with validation, save it to the database, possibly return errors,
4:55 and then at the end, instead of doing this bit right here, we're gonna do a redirect with flask.
5:03 So that's what's coming. Assuming that there are no errors. Okay, So here's our ad redirect view. You know why? Pyjamas unhappy with this?
5:13 But I guess it just write it this way, and then we'll be happy enough. I'd rather not see it upset there.
5:22 Okay, Super. So this is the view side of the story. And first little touch on the view method view models as well.


Talk Python's Mastodon Michael Kennedy's Mastodon