Welcome everyone to this lecture on redirects.
Sometimes a client user is going to provide a path that we want to redirect to another webpage on our
site.
This can actually be accomplished in Django through the use of the HTP response redirect function.
Let's imagine in our example of the website newspaper that we actually want to match the article pages
on the website to the numeric page in the physical newspaper that is that paper newspaper for readers
to actually find an article online.
For example, let's imagine a user has the physical newspaper in their hands and they're turning the
pages and they see Page two is finance.
Well, we would want them to be able to do is go to our website and then go to the first app for slash
two for that page number and then be redirected to first app forward slash finance.
So what we're going to do is show you how you could do that sort of redirect with Django, with what
we know so far.
Now an important note, the example we're going to show here is actually another typical way we'll redirect
pages.
We still need to learn about the reverse function, and you are all names and templates to really bring
this to a more robust way of redirecting.
In fact, what we're going to do right now has a significant flaw or error, which we'll talk about
in the lecture, and we'll actually induce it and then move on to the next lecture where we attempt
to fix this sort of conundrum.
But for right now, we're going to explore a very simple redirect syntax example, and we'll discuss
some more details behind it in the next lecture with reverse and neural names.
For now, let's head to our code editor and show you a simple redirect.
OK, so here I am at my views file and I also have URLs file underneath first app.
And essentially, what I want to do is create a view that can do the following If somebody were to type,
let's say domain that.
Com first app for its flash and then a zero, for instance, that would get redirected to the correct
corresponding topic.
So we are domain first app and then it would get redirected to something like finance or sports, etc.
So we're going to do is convert these keys into a list of topics and let's begin by creating our function.
So I'll have this be called number page view or NUM page view.
It takes in a request, as well as the number page that somebody asked for.
So we'll say in some pages Peston.
And then we want to do here is the following I want to grab all these keys from articles and create
a list from that.
So we're going to say that our.
Topics list.
So topics list is equal to a list of articles, keys.
OK, so we grab all the keys from that articles dictionary, create a list out of it, and this essentially
looks something like this.
It's going to be sports, comma, finance, comma and then the last string will be politics if we get
our order correct?
Yeah.
Sports, finance, politics.
So this topics list essentially looks something like this, which means I have the indices zero, one
and two for right now.
We're going to assume that a user is actually going to pass in the correct number page.
So from there, the actual topic that we want to look at would be our topics list.
At the index location for that particular number page and then we want to do is the actual idea of the
redirect.
So I can't really just send them to this page because that page doesn't really exist.
I want to send them finally to this page that has the topic name.
So I end up doing is first off, I have to make sure that I import.
If we go all the way up here and import HTP Response Redirect, which allows me to redirect to another
page on my website.
So also, you get the topic, I'm going to say return.
HDP response.
Redirect.
And then we're going to redirect.
To the topic, so we're going to save that change right now, something to do is take into account the
fact that we need to be able to actually search for this URL first before we start searching for this
one, which means I need to go to URLs that pie and set up that path.
So the first item in this URL patterns list is going to be path.
And then inside the string, I'm going to insert as an integer.
That particular page numb, let me check A's page on her page.
No, it was actually no page, so we'll switch that it's numb, underscore page.
So that's being passed in and then I want that connected to views, dots and then it's the number of
page views.
So page view.
Comma, there we go, so now that's our new URL patterns.
Go ahead and save that change.
And let me walk through what's going to happen.
So let's imagine a user literally types out this into your website.
Well, that ends up getting matched up with this path first, which means it then goes to our no page
view.
We look at that number page, look up the corresponding topic and then redirect to topic, which actually
ends up redirecting to this particular website page that looks something like first app for large topic,
which would then actually connect to this URL pattern to redirect to finally show, hopefully the correct
HTP response.
Again, something to keep in mind.
We're going to assume that they're going to pass in the correct index here.
Otherwise, you could always just raise a 404 error.
OK, so let's say these changes and make sure this is all working.
So I'm going to say Python managed up high.
I will run the server.
There we go.
So it's running right now.
I'm going to go to my homepage.
I'm bringing my browser, so I have home view.
I'll go to forward slash first app and let's check to make sure just let's say finance is working.
So I hit there.
I get finance on the finance page and let's see what happens if I say first app, forward slash, for
example.
One Enter.
And notice that we'll get redirected.
Let's do it for another number like zero.
Enter that gets redirected to sports page and then first app forward slash to gets redirected to politics.
If I do a number that's outside of that, like 100, I get this hate index error, which is kind of
interesting.
So List Index is out of range because there is no Index 100 here inside of that list.
So what I could do is come back to my one page view and essentially put this entire block of code inside
a try block and then have a four or four or redirect or some auto response if there was an error there.
So again, we can always incorporate what we've learned so far to fix that issue of that number page
not showing up.
The major issue right now is the fact that this is still a little too hard coded in as a redirect,
especially when we start introducing templates.
What I would really like to take advantage of is the fact that I can name you URLs and use what's known
as the reverse function to try to make this entire redirect process a lot more robust instead of having
to remember all these hardcoded topics and create a view like this.
So we're going to do is expand on this idea with redirects, as well as the reverse functionality in
the very next lecture, and we'll show you essentially how to do this correctly, so to speak.
So let's talk about that in the next lecture.