Adding a CMS to Your Pyramid Web App Transcripts
Chapter: Redirects and the admin section
Lecture: Keeping the query string for redirects

Login or purchase this course to watch this video and the rest of the course contents.
0:00 It looked like we had our redirect working really well. We've got our data now, granted, this should be more dynamic
0:07 and we should be able to build it out and edit it But assuming that we we're gonna get to that, we can go and ask for the existing redirects
0:15 These two here and it will take us over somewhere else So are we done? Well, not quite. We're not carrying all the information forward that we could
0:24 Let's have a quick look So over here, we can go to '/bytes' and it's going to take us to Pythonbytes.fm. Super, that works. Great!
0:32 However, let's have another look. What if we want to communicate to Python Bytes, that this is part of a marketing campaign
0:40 Or that the source came from us or any other information that they should process.
0:45 Some sort of information from the user and send it back somewhere else? That's usually done with this thing called a query string.
0:50 So often the URLs you'll see question mark a whole bunch of variables. That.
0:55 So let's just suppose this is a marketing thing we want to promote Python Bytes. We'll say utm-campaign that's used often by Google and other sites.
1:04 And then we said it's going to be equal to 'pypi-ad'. Maybe we ran an ad on PyPI and it's gonna go over to Python Bytes.
1:11 We want to tell Python Bytes, Hey, here's somebody that came from the ad you ran. Now let me copy that because we're gonna need it again.
1:18 What happens if I hit this? Yeah, No, we're not communicating that information over.
1:23 Notice it's just the plain URL. What do we do? We're throwing it away. So what we'd like to do is come up with.
1:29 So if we have it like this, we go there. We want the destination to also carry that forward.
1:34 Alright, so we're going to make sure that this URL does that. So what do we have to do? Well, the first thing we can do is, over here.
1:42 We're going to say if there is a redirect, we're going to send it over somewhere. Let's put, destination_url or something like that.
1:51 It's gonna be this here...like that. Now we're gonna also check for the query strings.
2:00 We're going to say 'if request.querystring' this comes out as just this part of it. 'events' something that was passed along
2:10 Then were going to update our destination to be the destination, question mark, the query string. Like so.
2:19 So we're gonna do that little check here, make sure that there's something going on, Alright.
2:24 If there is a query string, we're gonna do this. We don't want to always append the question mark. Only when there's a query string.
2:29 All right, let's try it again. Remember our goal is to end up with this. From... There we go. We want to use this utm campaign.
2:41 This is our query string. And let's just put one more up here just so we have all the data, you know, sort of the general case
2:46 because we could have multiple variables with multiple values So we'll have 'utm-source=fake-pypi'. Let's do those.
2:53 Okay. So when I try to get over to PythonBytes.fm, that. Here we go give it a shot. Yes, look at that. Beautiful! It worked exactly like we hoped.
3:05 Better question mark, key equals value, Ampersand key equals value. Let's just test one more thing. That was working.
3:15 But now, will it still work if we don't give it a query string? Will it like put the question mark or other weird things that shouldn't do? Nope.
3:23 If you give it just '/bytes', it takes you to PythonBytes.fm. If you give it a query string, it carries that forward.
3:29 All right, well, now the redirection side of our CMS is pretty close.
3:36 Another thing that we might want to do. We're not doing the moment, but we might want to do is record that we've received a redirect request.
3:45 so something that's really helpful is if I've got some URL and I share it on social media. How many people have clicked it right? If I say
3:52 Hey! Go check out the special post we've done or this episode we've done.
3:57 I would like to know how many times people clicked on that redirect to follow it over to that destination.
4:02 That might be really good for advertisers if this was an ad. You can do reporting to them.
4:07 If it's social media, I could know what of my social media campaigns are working well. All those kind of things
4:13 So doing something along the line where we actually store the fact that this request happened.
4:18 Maybe the ip address of the user where the request came from. So we can say by country. Here's the people visiting the different campaigns and so on.
4:27 We're not doing that right now. I don't know if we're gonna get to that later or not. I haven't quite decided yet. But this is really, really valuable
4:34 to just be able to use these tiny, short little URLs to go to something much more complicated. Let me run that out with a one more example here.
4:42 So we go over to talkpython.fm, and we go to '/YouTube'. So this is one of these redirects here.
4:49 I don't know why I don't want to keep it, but there it is. So if we go here? It's not just 'YouTube.com/Talk Python'.
4:56 No. We go here, It takes us to this really, really ugly url. So this lets us over at Talk Python say you want to go check out our YouTube channel,
5:07 all you gotta type is not even this right? You don't say that. You say 'talkpython.fm/YouTube'.
5:13 The reality of where that destination is, is quite unpretty. It's like this huge gross url that you would never expect anyone to type in.
5:22 But tell them the type talkpython.fm, sure. That's useful. And over at Talk Python, we even use that down at the bottom Like here's our YouTube channel
5:31 Oh, no. We put the full one. Sometimes we do put shorter things in here for stuff and sometimes not.
5:38 So anyway, having this nice little URL that we can use here that will take you somewhere much more complicated is valuable for a bunch of reasons.
5:45 And now our CMS has that built right in.


Talk Python's Mastodon Michael Kennedy's Mastodon