1
00:00:05,190 --> 00:00:07,680
Welcome everyone to this lecture on redirects.

2
00:00:08,700 --> 00:00:14,100
Sometimes a client user is going to provide a path that we want to redirect to another webpage on our

3
00:00:14,100 --> 00:00:14,460
site.

4
00:00:15,000 --> 00:00:19,620
This can actually be accomplished in Django through the use of the HTP response redirect function.

5
00:00:20,910 --> 00:00:26,610
Let's imagine in our example of the website newspaper that we actually want to match the article pages

6
00:00:26,610 --> 00:00:32,700
on the website to the numeric page in the physical newspaper that is that paper newspaper for readers

7
00:00:32,700 --> 00:00:34,380
to actually find an article online.

8
00:00:34,860 --> 00:00:39,420
For example, let's imagine a user has the physical newspaper in their hands and they're turning the

9
00:00:39,420 --> 00:00:42,750
pages and they see Page two is finance.

10
00:00:43,110 --> 00:00:48,340
Well, we would want them to be able to do is go to our website and then go to the first app for slash

11
00:00:48,660 --> 00:00:54,270
two for that page number and then be redirected to first app forward slash finance.

12
00:00:54,960 --> 00:00:59,550
So what we're going to do is show you how you could do that sort of redirect with Django, with what

13
00:00:59,550 --> 00:01:00,330
we know so far.

14
00:01:00,930 --> 00:01:05,220
Now an important note, the example we're going to show here is actually another typical way we'll redirect

15
00:01:05,430 --> 00:01:05,940
pages.

16
00:01:06,210 --> 00:01:10,710
We still need to learn about the reverse function, and you are all names and templates to really bring

17
00:01:10,710 --> 00:01:13,350
this to a more robust way of redirecting.

18
00:01:13,590 --> 00:01:18,150
In fact, what we're going to do right now has a significant flaw or error, which we'll talk about

19
00:01:18,150 --> 00:01:22,380
in the lecture, and we'll actually induce it and then move on to the next lecture where we attempt

20
00:01:22,380 --> 00:01:23,820
to fix this sort of conundrum.

21
00:01:24,270 --> 00:01:29,540
But for right now, we're going to explore a very simple redirect syntax example, and we'll discuss

22
00:01:29,540 --> 00:01:34,020
some more details behind it in the next lecture with reverse and neural names.

23
00:01:34,140 --> 00:01:37,710
For now, let's head to our code editor and show you a simple redirect.

24
00:01:38,610 --> 00:01:44,160
OK, so here I am at my views file and I also have URLs file underneath first app.

25
00:01:44,580 --> 00:01:49,530
And essentially, what I want to do is create a view that can do the following If somebody were to type,

26
00:01:49,530 --> 00:01:50,410
let's say domain that.

27
00:01:50,450 --> 00:01:57,360
Com first app for its flash and then a zero, for instance, that would get redirected to the correct

28
00:01:57,360 --> 00:01:58,700
corresponding topic.

29
00:01:58,710 --> 00:02:05,940
So we are domain first app and then it would get redirected to something like finance or sports, etc.

30
00:02:06,300 --> 00:02:14,250
So we're going to do is convert these keys into a list of topics and let's begin by creating our function.

31
00:02:14,850 --> 00:02:18,390
So I'll have this be called number page view or NUM page view.

32
00:02:18,930 --> 00:02:23,850
It takes in a request, as well as the number page that somebody asked for.

33
00:02:23,880 --> 00:02:25,530
So we'll say in some pages Peston.

34
00:02:27,070 --> 00:02:32,230
And then we want to do here is the following I want to grab all these keys from articles and create

35
00:02:32,230 --> 00:02:32,950
a list from that.

36
00:02:33,520 --> 00:02:35,200
So we're going to say that our.

37
00:02:36,760 --> 00:02:37,750
Topics list.

38
00:02:39,510 --> 00:02:45,600
So topics list is equal to a list of articles, keys.

39
00:02:46,290 --> 00:02:51,480
OK, so we grab all the keys from that articles dictionary, create a list out of it, and this essentially

40
00:02:51,480 --> 00:02:52,380
looks something like this.

41
00:02:52,800 --> 00:03:00,480
It's going to be sports, comma, finance, comma and then the last string will be politics if we get

42
00:03:00,480 --> 00:03:01,290
our order correct?

43
00:03:01,300 --> 00:03:01,470
Yeah.

44
00:03:01,530 --> 00:03:02,730
Sports, finance, politics.

45
00:03:03,060 --> 00:03:08,430
So this topics list essentially looks something like this, which means I have the indices zero, one

46
00:03:08,430 --> 00:03:09,750
and two for right now.

47
00:03:09,750 --> 00:03:12,880
We're going to assume that a user is actually going to pass in the correct number page.

48
00:03:13,620 --> 00:03:19,410
So from there, the actual topic that we want to look at would be our topics list.

49
00:03:20,270 --> 00:03:27,350
At the index location for that particular number page and then we want to do is the actual idea of the

50
00:03:27,350 --> 00:03:28,010
redirect.

51
00:03:28,340 --> 00:03:32,540
So I can't really just send them to this page because that page doesn't really exist.

52
00:03:32,780 --> 00:03:36,410
I want to send them finally to this page that has the topic name.

53
00:03:36,980 --> 00:03:40,460
So I end up doing is first off, I have to make sure that I import.

54
00:03:40,490 --> 00:03:47,600
If we go all the way up here and import HTP Response Redirect, which allows me to redirect to another

55
00:03:47,600 --> 00:03:48,620
page on my website.

56
00:03:49,160 --> 00:03:51,890
So also, you get the topic, I'm going to say return.

57
00:03:52,520 --> 00:03:53,750
HDP response.

58
00:03:53,930 --> 00:03:54,800
Redirect.

59
00:03:55,190 --> 00:03:56,990
And then we're going to redirect.

60
00:03:58,220 --> 00:04:04,520
To the topic, so we're going to save that change right now, something to do is take into account the

61
00:04:04,520 --> 00:04:11,120
fact that we need to be able to actually search for this URL first before we start searching for this

62
00:04:11,120 --> 00:04:15,350
one, which means I need to go to URLs that pie and set up that path.

63
00:04:16,040 --> 00:04:19,820
So the first item in this URL patterns list is going to be path.

64
00:04:21,610 --> 00:04:24,520
And then inside the string, I'm going to insert as an integer.

65
00:04:25,560 --> 00:04:29,100
That particular page numb, let me check A's page on her page.

66
00:04:29,130 --> 00:04:34,920
No, it was actually no page, so we'll switch that it's numb, underscore page.

67
00:04:35,250 --> 00:04:41,730
So that's being passed in and then I want that connected to views, dots and then it's the number of

68
00:04:41,730 --> 00:04:42,370
page views.

69
00:04:42,370 --> 00:04:44,340
So page view.

70
00:04:45,710 --> 00:04:49,250
Comma, there we go, so now that's our new URL patterns.

71
00:04:49,700 --> 00:04:50,990
Go ahead and save that change.

72
00:04:51,380 --> 00:04:53,570
And let me walk through what's going to happen.

73
00:04:53,960 --> 00:04:58,190
So let's imagine a user literally types out this into your website.

74
00:04:58,310 --> 00:05:06,050
Well, that ends up getting matched up with this path first, which means it then goes to our no page

75
00:05:06,050 --> 00:05:06,410
view.

76
00:05:06,860 --> 00:05:13,070
We look at that number page, look up the corresponding topic and then redirect to topic, which actually

77
00:05:13,070 --> 00:05:20,060
ends up redirecting to this particular website page that looks something like first app for large topic,

78
00:05:20,420 --> 00:05:25,850
which would then actually connect to this URL pattern to redirect to finally show, hopefully the correct

79
00:05:25,890 --> 00:05:27,050
HTP response.

80
00:05:27,530 --> 00:05:28,580
Again, something to keep in mind.

81
00:05:28,580 --> 00:05:30,950
We're going to assume that they're going to pass in the correct index here.

82
00:05:31,250 --> 00:05:33,260
Otherwise, you could always just raise a 404 error.

83
00:05:33,800 --> 00:05:36,770
OK, so let's say these changes and make sure this is all working.

84
00:05:37,810 --> 00:05:40,360
So I'm going to say Python managed up high.

85
00:05:41,500 --> 00:05:42,550
I will run the server.

86
00:05:44,200 --> 00:05:44,680
There we go.

87
00:05:44,710 --> 00:05:45,910
So it's running right now.

88
00:05:46,390 --> 00:05:48,670
I'm going to go to my homepage.

89
00:05:48,880 --> 00:05:51,430
I'm bringing my browser, so I have home view.

90
00:05:52,090 --> 00:05:58,000
I'll go to forward slash first app and let's check to make sure just let's say finance is working.

91
00:05:58,480 --> 00:05:59,190
So I hit there.

92
00:05:59,200 --> 00:06:04,300
I get finance on the finance page and let's see what happens if I say first app, forward slash, for

93
00:06:04,300 --> 00:06:04,810
example.

94
00:06:04,810 --> 00:06:06,310
One Enter.

95
00:06:06,490 --> 00:06:08,130
And notice that we'll get redirected.

96
00:06:08,140 --> 00:06:10,090
Let's do it for another number like zero.

97
00:06:10,870 --> 00:06:17,200
Enter that gets redirected to sports page and then first app forward slash to gets redirected to politics.

98
00:06:17,470 --> 00:06:23,140
If I do a number that's outside of that, like 100, I get this hate index error, which is kind of

99
00:06:23,140 --> 00:06:23,620
interesting.

100
00:06:23,890 --> 00:06:30,890
So List Index is out of range because there is no Index 100 here inside of that list.

101
00:06:30,910 --> 00:06:38,710
So what I could do is come back to my one page view and essentially put this entire block of code inside

102
00:06:38,710 --> 00:06:45,100
a try block and then have a four or four or redirect or some auto response if there was an error there.

103
00:06:45,490 --> 00:06:49,930
So again, we can always incorporate what we've learned so far to fix that issue of that number page

104
00:06:50,140 --> 00:06:50,860
not showing up.

105
00:06:51,370 --> 00:06:57,490
The major issue right now is the fact that this is still a little too hard coded in as a redirect,

106
00:06:57,670 --> 00:07:00,070
especially when we start introducing templates.

107
00:07:00,400 --> 00:07:05,290
What I would really like to take advantage of is the fact that I can name you URLs and use what's known

108
00:07:05,290 --> 00:07:12,070
as the reverse function to try to make this entire redirect process a lot more robust instead of having

109
00:07:12,070 --> 00:07:16,600
to remember all these hardcoded topics and create a view like this.

110
00:07:16,720 --> 00:07:22,540
So we're going to do is expand on this idea with redirects, as well as the reverse functionality in

111
00:07:22,540 --> 00:07:26,980
the very next lecture, and we'll show you essentially how to do this correctly, so to speak.

112
00:07:27,070 --> 00:07:28,930
So let's talk about that in the next lecture.

