Hello everyone and welcome to this new lesson about the EPA view class in the first lecture of this
section.
We have mentions that January's framework uses many of Jangles programming conventions and one of the
most striking examples is its use of class based views as we've pure Django.
Using this kind of pattern makes it easy and fast to create reliable web apps and by reusing common
functionalities it also helps us keep our code dry which means in line with they don't repeat yourself.
Principle of software development in this lesson we're going to start understanding our glass based
views are used in the context of REST API development by using django as frameworks API view class.
Even though at a first glance the code we will write might look similar to the code we have already
written in the previous lecture.
It is important to note that the API view class is the class upon which all the generic class based
views that we'll introduce later are not based.
Therefore it's important to make some examples of its use.
Let's get started.
Okay.
So here we are invisible to your code.
And the first thing that we need to do is to update our import statements.
And that's of course because we need to import the API view class but we're then going to use to create
our class based VBA views.
So from this framework that views import API view.
Let's now take a look at the class definition took it the understanding of how awesome this class release
is you can see we got a bunch of methods as one might expect.
And that's because our class is particularly powerful because of all three properties that we can set.
See we got a lot of properties such as the Renderer classes but asset classes but also what integration
classes throttle classes per mission classes and so on.
Now we can define to personalize automatically basically Debbie a viewer of our class now as a matter
of fact we are not going to use these parameters these attributes right now and that's because as we've
said this class is used as a base for a lot of other classes that we're going to use in great details
later on during the course the purpose of these specific lecture is to give you an introduction on how
we can actually create API views using django as framework so that then later on as we introduce all
the other classes that are based upon this code you're going to be able to get a proper complete and
deep understanding of our day work so we're not going to write two classes article least create GPA
view and article detail API.
If you basically will go into right to the same kind of functionalities we're going to write down the
same functionalities this time using classes and we have the same functionalities clearly.
I mean the same functionality that we've developed let's say in that previous lecture.
So I'm going to go right below here and I'm going to define our first class.
So class.
Article list create API in view which is going to extend our API view class.
Like so and to provide functionality to our class.
This time we're going to use class methods and each method is going to correspond to the H GDP verb
that is actually going to power our End Point.
Okay so we're going to have a get method which is going to take self of course and the requester.
So the code is going to be the same.
We've written right here.
Like so when the fixed indentation.
Perfect.
And then I'm going to define a post method which is going to accept self and request.
And the code is going to be basically the same that we've written up here.
Like so so even though the code is basically the same that we've written with the function based API
views can see out now it's more organized the class definition makes it more concise.
Let's now write the detailed view straight away so class vertical detail API view.
She's going to extend clearly maybe a view like so and considering that up here you see we've actually
written a try except the blocker right here on the top of our function.
We can now define a method a get object method which is going to return the instance that we are looking
for.
So def get object which is going to accept self and a primary key like so.
So particle equals and considering that we are making an example of how to use the API view class this
time we can actually use the get object or form for a shortcut.
So I'm going to import it
from the last framework not generics import get object or photo for therefore particle is going to be
get object or for a form of article with primary key equals primary key and we are now going to return
the article instance and we can now write our mind so so def do get method which is going to accept
self request and a primary key.
So first of all we need our article instances so article equals self doubt and get object.
Now we're going to pass it the primary key.
We can then call civilized at every turn.
It's data like so we can do something similar for the other methods.
So Def put up which is going to accept self request and primary key and as a matter of fact is going
to need in the article as well we can then go ahead and copy the code we've written right here
like so perfect.
Same thing we can now do for the delete metal so def delete which is going to accept self request and
the primary key we get not after instance we can now delete things.
This like so and threat to earn an appropriate response HDB what for not content.
So now that our views are ready we can go ahead and define the two corresponding endpoints.
I can't comment this impulse statement right here and I can use the same two endpoints as a base to
build the new ones that we are going to need.
So first of all we need to import the new classes so I can in fact copy this one as well.
So but this time we don't put it in our classes.
So one detail API if view here and at least create API view perfect.
We can now connect them to the appropriate end points.
We need to use as view seeing think for create a view that s view and you can also change the indentation
to beat like so what.
And to perfect now the starting points that are completed we can around the development server and check
out a work.
So let's go back.
To Chrome and let's check.
D first and point.
Everything works as expected clearly.
It's great then grab the details of a specific instance.
Perfect and maybe we can also create a new one.
I'm going to go to the Lesh API select articles.
I'm going to delete create that and update that.
Same thing for the primary key second Article V arrest API
post and edit is perfect everything works as expected.
OK.
That was it for this lecture about the API view class in the next lecture.
We're going to start talking about a very important topic validation in January's framework.