So let's now focus on building a second API view.
So Def article detail API view which accepts a request and a paramedic key they were going to use to
know which instance we want to work with.
Now that we know how to use the API view decorator we can start by dictating our view.
So API view we're going to pass a list.
And so as we did for our article least create API view.
This list is going to include all the ATP verbs that we want our API view to be able to manage.
So in this case I'm going to just get put and delete because using these API view we want our users
to be able to retrieve data about a specific instance or update the same data or delete these steps
altogether.
So let's start to write some code.
First of all we want to know if the primary keyword is being passed via our End Point is actually valid
which means that if it's valid our database is actually going to contain a record which will correspond
to the primary key that is being passed.
We could use get object or for a fourth.
There is also a function that is provided by jingoist framework a specific version of Get object or
for a fourth.
However I believe it's better to write a couple more of lines in this specific case to actually line
out the eventual error message.
The way we wanted to be so we're going to use a try except clause to write article equals article dot
objects that get with primary key equals primary key except article that does not exist.
In that case we return a response and we define a dictionary with the error message basically that we
want to show so error and then another dictionary which is going to include the code.
Remember this is for human consumption.
So code for a for message article not found.
And that's enough for the error message.
Maybe you can just unintended like so and we can help us started as a sort of status equal status dot
HDP fault for not found.
Now if the article exists instead we can start to check the kind of request that has been made and based
on that provide an appropriate response.
So if request that method equals get it then we get out to realize it and I said equals article utilize
it and we feed our article to it and then we just return it.
C realize that just data like so any request that method equals full term which means that a user wants
to update a specific record we take us to that.
So either.
It was pretty close utilized but this time we also pass data equals request dot data we check if C realize
that dot is valid.
Remember we always need to check if cedar I's that dot is valid.
And in that case we call C realize that dot save which is going to trigger the update method and that's
why will surpass article and then we're going to return a response based on serialize the DOT data.
Otherwise you serialize the return an invalid response basically false with no return address and starts
a 400 bed request then Elif requests that method equals delete.
In that case we want to call did delete method on the instance.
So particle dot delete.
And then we're going to return.
Response.
Status equals status dot age GDP 2 0 4 not content.
Perfect.
We have now ready to actually define an other end point.
So first of all we need to import a new view like so and stay in alphabetical like would actually invert
the position and maybe even jump a line like so I can now create the second end point so articles slash
integer primary key.
This time we want to call practical detail EPA view and the name is going to be critical detail.
Of course very well we can now restart our development server and let's go back to Cairo.
So let's try and get the details of or maybe the second article.
Okay so I'm going to go to API articles.
The primary key to and let's see what we get back.
Perfect as you can see we get the details of the article with primary key to we see dead DNA bind allows
delete methods options get and put it and we can actually three good options just by clicking here on
options and you see we get the details of the renders and passes.
So what's this end point renders and what it passes and you see we get Jason but also HDMI all because
of the browser API.
Same thing for Jason X W W W for you et al encoded and most importantly form data because of the browser
bool API.
So let's try and update our instance we need to pass the J So like so I'm going to remove created it
updated it and idea that I'm going to update maybe the body so some content updated like so.
So now clicking on PWD we're going to make a put request towards our end point possibly these data telling
DC that I there basically that we want to update a specific record we've the data we've just passed.
So let's try it put and if we get it.
You see everybody has been updated and if we now go and make a get request to the basic end point API
is less articles.
We see that now the record with primary key number 2 has been updated.
Perfect.
We can all make our last test.
Let's go to the same endpoint with the same primary key.
Let's make it delete request towards the same end endpoint so delete it.
Are you sure you want to delete this article detailed API.
You basically using the name of our view and yes we're sure we want to delete this specific instance
deletion completed we get back HBP 2 or for no content perfect and if we now go to our list and point
we see now we've got only two instances the one we've primary key one and the one we've primary key
3.
Perfect.
In fact we can also make a very last to test it going and trying to grab the details of an article that
doesn't exist.
For example one way for primary key for how you see we get or four not found.
We've added our code photo for our message.
How do think are not found.
Everything works as expected.
Okay perfect.
That was it for this lesson about the view decorator in the next lesson.
We're going to see how to use the API view class to create class based EPA views.
We've Django this framework.