Welcome back, everyone.
And this lecture will be discussing dynamic views and logic.
We're now going to explore two key ideas.
One is just adding in simple logic to a view that is connecting Python code rather than just send back
a string through an carp response.
And the second one is a very important idea, which is creating dynamic paths that is having that actual
Yoro Path route defined by the client.
Let's check this all out in Visual Studio.
All right.
Here I am back at our code right now we have one simple view and that's connected to URLs patterns or
you're pie inside the application here.
Path views that simple view and then that is connected to our project level here under path first app
include first app URLs.
So if I were to go to my domain name forward slash first app, I get the actual result.
Simple view here, my browser.
Now let's imagine we've been inspired by the newsroom sources of Django, so the fact that it started
in NEWSROOM and I decide that I'm going to create a newspaper online, so I'm going to make actual several
views.
Let's make one for the sports page, so we'll have a sports view.
And then this one's just going to say something like sports page.
And then what I'm also going to do is create another view, and I'm going to call this view, let's
say our finance view for financial news, so have this won't be a request as well.
And then that's going to send an HTP response.
And this is going to say something like finance page.
All right.
So we have a sports view for a sports page and a finance view for the finance page.
And what I need to do now is actually connect these multiple views to a URL.
So I'll come back here and we'll have one go ahead and be sports for us, and we'll connect that to
the sports view.
And then I'll add in another path connection.
And this one's going to be finance forward slash, and we'll connect that two views Dot and then we
have a finance view if I come back here or call it, have finance for you, so we'll connect it to finance
you.
There we go.
So I can save that change and then I'll come back here and actually if I refresh this now, I'll get
nothing found since not only have admin, first up, sports, first up finance.
So let's check that out.
I'll go first.
App Sports and I see the sports page.
I should see the same for finance hit enter and then I have that as well.
Now, one issue with this is for every new topic I want to add.
I have to create a new view and then create a corresponding new URL.
Let's see if we can be a little smarter about this.
For instance, one thing I can do is take advantage of Python code, so either by importing another
Python file or by writing this itself inside of.
Maybe I'll just have something like the articles.
Be formatted in a dictionary and then I'll do something like this.
Sports.
And I'll have this be the sports page.
Comma, and then here, I'll say finance and I'll have this return.
Finance page comma.
And then let's have one more, let's say, for politics, and I'll have this one return back the politics
page.
And then I could try being a little smarter here and then say it's the poor response instead of just
typing in sports page.
I'll say something like articles.
And then grab sports and then here I could say articles.
And then grab everything to do with the finance key.
So the one thing I want to get across right now is while we probably wouldn't do this in a real website
is the fact that I'm starting to mix in python objects like a dictionary to my views.
And if I were to come back here and start refreshing this, you'll notice that finance is working OK?
Sports is also working, OK, but remember I right now they'll actually have a view on your connection
for politics, which means if I come here and type out first app slash politics, then it's not working.
Now, clearly, as we've mentioned before, it'd be nice if I was able to dynamically update this.
That is not to define every single possible type of view here.
Instead, just to find something in a Python object and later have that sync up based off what I'm actually
looking for.
So how can I do that?
We can use dynamic routing in order to create this.
So I'm going to close this off or delete this and create a single view called News View, and it won't
just take in a request, but it's also going to take in a topic.
And this way, I can do the following I can say that my topic is going to be what I pass in here in
order to actually get an HDP response.
So I pass on a topic so it could be sports, finance or politics.
And then that's going to retrieve and create the news view.
Now I do have one issue is the fact that I technically do not know this topic ahead of time.
The user or client is going to type that in.
It's going to type in the domain for its first step forward slash and then some sort of article topic.
So what I need to do is make Django aware that this topic needs to also be dynamically form for the
URL pattern.
And luckily, Django makes that quite easy.
We come to Earl's dot pie and then instead of defining a path for every single possible article type,
we're just going to say the following I'll say path and then the way we actually tell Django that this
is going to be dynamically defined by what's actually passed in to the view here for a topic is with
the following syntax.
We use these two races or brackets that is less than and greater than sine.
And then I can pass in topic and connect that to views that.
News view, and then we want this to be a topic.
Forward slash.
There we go.
And so now what this is saying is the user can pass in whatever they want into the news, view whatever
topic they're looking for.
We're going to look it up sports, finance or politics.
And then we're also going to define that dynamically in your old patterns as topic so I can come back
here.
Recall politics used to not be found.
I'm going to refresh this and now I see the politics page.
And if I try sports enter, I now see the sports page.
Now we still have the issue that if something doesn't exist, so we currently don't have a section or
a website for, let's say, the arts.
So if I type in first app for arts enter, then I get this error.
Hey, there was nothing there for arts.
So later on.
We're probably going to want to use some sort of Easter to be response to redirect them in case we're
looking for something.
And right now, you'll notice we actually get a key error that's indicating that the error is actually
happening on the dictionary side of things, not really on the jingo side of things, although it's
technically all connected.
But what I do want to show you to finish off this idea of the integrated URL patterns is the fact that
I can pass in this dynamic variable and use it both on the Python view side of things and dynamically
update the URL patterns.
The last thing I can also do is what's known as a path converter, so you can use a path converter to
specify the Django that you want a particular variable to be treated, let's say, as a string or as
an integer, and you can check out all different path converters on the Django documentation.
We'll talk about these in more detail later on, but if you go to the documentation on the URL dispatcher,
you can see there will be a section on path converters.
And you can actually pass this in a saying make sure it's a string or make sure it's an integer, a
slug.
We'll talk about that later on, but it basically puts in these dashes into longer strings.
It can be a unique ID or can just be path.
So we'll talk about path converters later on.
But I do want to show you their power, essentially informing Django, Hey, it's going to be a string
or it's going to be an integer.
So something here that we won't actually see the effect of, but I could inside those brackets percent
SDR colon and this will make sure that topics is going to be a path converter for a string.
To show you another example of this, we're going to do something slightly more complex to finish off
this lecture, so I won't come to views that PI and I'm going to create a view.
Cardiff.
That view takes in a request and then takes in two parameters.
Number one and number two.
And then what I'm going to do is I'm actually going to calculate the result of adding number one plus
number two, and these are going to be treated as integers.
Keep in mind, I need two percent integers here and then I'm going to return an HDP response of the
string result of that results here.
So what I'm going to try to do is the following I want the user to be able to visit my domain and underneath
first app, they can type in number one, forward slash number two, and then the result is actually
going to be number one plus number two, just to give you an idea of how dynamic this can be.
So, for example, if the user goes to first app and then type in three forward slash four, then the
result on the web page should be seven.
So actually adding three and four.
But to do that, I need to make sure that Jeong understands these are integers, so I'm going to save
Advil.
And then let's add it in dynamically.
So we're going to say comma here path.
And then I'm going to define integer.
No one.
Bracket and then forward slash integer colon, and then we're going to say no to bracket and we have
to actually make sure this all goes within the same string.
So let's delete this quote here, and you can see this dynamic routing is essentially what I was defining
here.
Three comma four or number one.
Number two, that it actually means they come at forward slash.
So we have first that forward slash number one for System two.
And you can see here we have.
Number one, number two, we just need to connect the view, so say views, dot and then this ad view.
OK, so there we have it, let's see if this work is going to come back here and let's go first app
and let's try it first app forward slash three forward slash four enter and you can see I get seven
just to make it really clear.
What I could also do is the following.
I could say that my result is actually equal to the let's actually say this as a result.
So I'll say this is the end result.
And then I could say the following here.
I can make this a string, and I could say.
No one.
Plus.
Numb to.
Equals and then just pass in as a result, and that's going to be some formatting there.
So let's save that change and if I refresh this.
I shall now see three plus four is equal to seven.
And you can experiment with these.
Obviously, it expects this to be integers, but you get the idea 10 plus 12 equals twenty.
So hopefully that gives you an idea of the power of dynamically updating these views and starting to
connect them with the idea of just normal Python objects.
OK.
So let's quickly review what we've done so far.
We understood that I can start integrating the idea of different python objects within views and start
using that Python code and logic.
And then the other thing now I understand is the fact that I can have user dynamically updated routing
and views.
So I pass on topic here, and I just need to understand that using these brackets of less than greater
than science is the way I can update paths dynamically.
So that's the main thing is to get from.
This lessens the fact he can update those paths dynamically.
However, we still have the issue of what happens if that page doesn't exist.
What do we do?
We're going to cover that with four or fours and redirects in the next lecture.
I'll see you there.