Adding a CMS to Your Flask Web App Transcripts
Chapter: The redirects (our bit.ly)
Lecture: Keeping the query string

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now let's jump back in here and quickly review our CMS and specifically how we've done
0:06 redirects. So remember we threw a really quick example of we have some data of redirecting while we're doing the pages.
0:13 Let's just throw that in and you might be wondering, Well, Michael, why is there a whole chapter on redirects If we're basically done
0:20 As you can see here we are technically doing the redirect. There's more work to do here to make it really rich and full featured two things
0:28 in particular, but nonetheless it's basically done. So what's this chapter about? This chapter is more about the content management system side
0:37 of things rather than redirects itself in our CMS, we're going toe, have the ability to create and manage two types of things.
0:46 Redirects and pages redirects are pretty simple. There. Start your old neural on a name.
0:51 And so having in management system around those read Rex is simpler than,
0:55 say, pages with cashing in multiple formats like markdown versus HTML and all this kind
1:03 of stuff. So we're gonna focus in this chapter on building the management side of our redirects. But before we dig into that,
1:10 there is something missing and I want to work on that. So let's go over here and have a look at our site running and remember that
1:16 we could type slash bites and this would take us over to the Python Bites podcast And it does recall that request comes in.
1:24 We're using our path. The very end of catch all the requests it comes in here says they're a page called bites.
1:29 Nope, nope. Age. So we skipped out here. Is there a redirect called bites? Yes. So it's blast, not redirect over to the stored Your l Here.
1:39 This is cool. However, there's some problem. What if we're running Pipi I and the podcasts by them bites wants to run an
1:51 ad over here or some kind of promotion, Or somehow we want to communicate from our site additional information to them.
1:59 So we might do that like this with a query string. So if this was let's just say it's an ad,
2:03 it should be you tm source equals maybe bake pie p I and we want to send this over to Python bites, right? Copy that, because it's not gonna work.
2:18 We're gonna need it again. So if I hit go, it's gone. What we expected what we wanted was that when we sent that information
2:28 there, this could come across like that. So then on their side, that destination site, they would know they received a request from us.
2:39 But that didn't happen, did it? It just went there. And it just strip that off just like this.
2:45 So what we want to dio is we want to update our system to carry this query string forward. Turns out that's not too hard to dio.
2:53 So what we can do is come in here and say, If there is a redirect, we can create a destination to be just that right
3:01 there. And then we can ask if flask not request, not query strength. If there is a query string, we wanna add it on to the destination.
3:10 So if we have a query string here, carry that forward. So we'd say the destination is when I create new F string here. Say it's the destination.
3:20 Question Mark. We'll ask that request that queries drink. Maybe we want to shorten that or something, but that should do. All right.
3:30 Hopefully I got that right there. I think so. Let's give it a shot. Let's save flask should rerun. Yeah, it did. So come over here.
3:41 Give it a shot. Will it carry through? And will it come through correctly? Oh, whoops. I forgot that. That's a bite, not a string.
3:50 So let's go over here and fix that really quick. Let's just turn this into a variable cause this is getting longer longer.
4:00 It's called that query. I'll say we were actually went to query to not be just the bites will say query equals query dot decode and I got to give it
4:10 the format. Utf eight or something like that. Here we go. So now it comes Bites turns into a string. Utf It's a pretty safe bet.
4:18 Let's try that one more time because we don't have this b and these quotes. That's wrong. All right, so it would come up here and just do bites
4:27 like this. Are you tm source Perfect. Look at that. It passed it along with Got our question mark And are you
4:35 tm source equals something. Let's do one more test. Let's go over here and say I'm sure we can carry multiple values.
4:42 Cory strings can have more than one key equals value. So let's say you tm campaign equals pi p I something like that.
4:52 Okay, so we want to make sure that both of these get carried across and of course, they dio final thing to test.
4:58 To make sure that we have here is Well, you want to just request it directly like this with nothing. We want to make sure that,
5:06 like the question mark doesn't come across with an empty query string or something like that Let's give it a shot.
5:11 Perfect. So it looks like our redirect keeping the query string it exists. Assuming you want to do that,
5:18 I think that you probably would almost all the time we dio and so we're going to do it here. So this is really great.
5:26 And, you know, it's you may be wondering, like, what is the real value of this? There's a couple of things that you can dio one.
5:31 It allows you to have much, much simpler Urals. You can use thes for like social media.
5:36 You can use them on places where you might have to write something down. So, for example, over on talk Python dot FM If we want to talk
5:45 about our YouTube channel, we can just lay slash YouTube like this and it takes us to some terrible Earl that you would never,
5:55 ever never expect someone to type that in. You wouldn't say youtube dot com slash channel slash capital u Capital Seacat,
6:04 right? You just wouldn't do that. So it's very easy for us to say Talk by fun that offense last YouTube and talk about it. So this is really valuable.
6:11 The other reason that having these redirects is great is you can every time somebody runs of this part of code here,
6:18 they make a request that is a redirect. You can record that that your l was requested.
6:24 So, for example, over on talk by thought Donna fem or buy them bites we have ads. We have companies sponsoring us,
6:32 and we want to make sure that we can give them reports about how many times people visited there. You, Earl,
6:38 whether we just spoke it out loud on the podcast or it was in the ad image and they clicked it, or if it was shared on social media and clicked
6:46 it. What we also do over there is we record into the database that one of these Urals whatever one to come up for this redirect was requested.
6:55 We record things like what? Um, operating system people are on or things like that.
7:00 So really, really nice. A lot of features you can pack into this bit here are active, doing the redirect is done,
7:09 but this chapter is just getting started because what we're really building in this section is the first part of the management system,
7:16 right? These redirects that were requesting they're coming from hard coded stuff that's in the
7:22 source code. What we want is users marketing people and other folks toe log into the website, type into it,
7:30 and then we have additional redirects for additional pages that they can go to, right? So that's what we're gonna dio.
7:36 From now on, we're gonna allow people to create and manage these redirects without getting


Talk Python's Mastodon Michael Kennedy's Mastodon