Hello everyone and welcome to this new lesson about the EPA view the.
Now that we've learned how to use it allows us to serialize and this serialized data it's finally time
to write our first EPA view to use them with January's framework provides two rappers we can use to
write EPA views.
One of these is the EPA view decorator for working with function based EPA views.
The other one is the EPA view class for working with class based EPA views because of this wrappers
out of use we get for free.
All the code that is necessary to receive requested instances provide an appropriate response and handle
exceptions such as the pass air or exception that occurs when access request does data with malformed
input.
In this lesson we're going to create two EPA views that will provide.
Read their right functionalities for other articles using the EPA view decorator.
We're also going to introduce one of January's frameworks most cherished and useful features.
The browser will API.
Let's get started.
Okay so here we are invisible to your code.
And the first thing that we want to do is to create the views that Pi Phi within the API folder we've
created in the previous lesson so that we have a specific file to create our API views within.
We could use the views that Pi Phi that's been automatically created by Django when we've created the
news app.
However creating all the files that we need within the API folder is a much better choice considering
that it allows us to keep a much more consistent structure within our project.
So new file views that by we can close models that by and see that either stood by for now and the first
thing that we want to do of course is to make all the imports that we're going to need to create our
API views.
So first of all we're going to need starters.
So from this framework import status then we're going to need DARPA view decorator.
So from this framework dot decorators input API view then we want to import a response.
So from this framework that response.
Import response and then clearly we're going to need to import also our article more than in the article
serialize that class.
So from news that models import article and from use do the API dot serialize this import article
serialize it.
So we will really said that in this lesson we going to create two API views that will provide read and
write functionalities for our articles model.
And I think we can start by creating day least create EPA view for that article.
So the effort is going to define the function article at least create API view which is going to accept
requests for all the function based views.
So the first legit question you might be asking yourself is how can our view decide which kind of functionality
to provide.
I mean how does it know that it has to create a new instance of article or it has to show a list with
all the articles in our database.
And the answer is that is going to know that based on the kind of request that is being called by.
Remember that we said that in the rest architecture a Rest Api provides different kind of functionalities
based on the request that the client is making to the API and we actually have to define the kind of
functionality that our view will be able to manage passing.
At least we have dead different kinds of requests to the API view decorator and we do it like so.
So we decorate are a function API view and we pass the list with the methods.
So let's start by passing just to get method we can all check if request that method equals get it.
And in this case we can act accordingly.
So articles we get a query said we've all the articles that we want to show.
In this case all the articles that have been flagged as active.
So article objects dot filter active equals through and we can now feed adequately set to the articles
serialized in order to get the data from the serialized it.
Then we can return as a response.
So serialize it equals articles utilize it.
And we can feed it adequate he said.
What do you think is this going to be enough.
We'll pass in a query set to our article see realize it.
Let's see what happens.
Return our response.
We said that we want to return to realize that not data.
I need to fix this multiple year its response not risk pose.
And we now need to connect our article least create EPA view to a U.S. rail path.
Basically we need to create our first API and point so I'm going to create you arrested by within the
API folder and yet I can import path and so from Django don't you or else import path and you also need
to import article least create API view.
So from use the API that views import
out of your function we can now create.
Are you allowed partners list so path and we want to pass the End Point articles and so on articles
we're going to provide both read and write functionalities for our articles.
So article least we can just call it article list.
We now need to connect the file to the main you had stood by file.
So I'm going to force include.
And here we can do API.
So on the API path we want to include when to include use dot API not to address.
We can now run our development server
and now that it's up and running so we can go to Chrome and test our end point so clearly we've got
no home page let's say.
So yeah we get.
Page not found a forum for error message but we've see clearly that we've got an API endpoint.
So let's go through Lesh API.
Articles you'll see we get an error attribute editor at Slate API Lesh articles got attribute error
when attempting to get a value for field author on single eyes that article see that I said this you
realize that a field might be name incorrectly and not match any attribute or key on date where he set
instance.
So that's the point we're feeling that way is set to are CDI is that therefore we get an error.
What do we need to do to fix this error is to pass another parameter to want to see the light is it
which is mainly equals through so that Aristide allies it now knows that he needs to serve a whole query
set so it lets reloads development server let's go back to Rome and let's make another in fact get a
request to our articles and point and E.D. adequately set at least we have the two articles that are
available in our database.
And as you can see content type application Jason what you are seeing here is Django frameworks browser
able API.
And it's basically a web interface that's used for development purposes which is you can clearly see
it's very comfortable for us developers we see which kind of request we're making on which end point
we get a lot of information on the request itself.
You see we got the status quo 200.
OK.
The kind of methods that are allowed on these specific end point.
And as you can see the Jason has been formatted so that is now ready for human consumption.
Let's say we can also get the standard Jason format.
But in general there won't be interface is the interface that will want to use most of our times.
And it's important to note that such a browser API is not available in all web frameworks at all.
Typically you would have to use a program such as postmen to communicate with your API.
But as you can see luckily we've January's framework.
It is not needed.
We are where we are going to use postman in one specific lesson later on during the course.
But just to make an example we're going to use the browser API from now on.
So let's now go back to visual studio code.
And before we start writing the rest of the code I want to make you notice that here we have not used
Jace on response as we did with the pure Django API we've built in the preview section here.
Clearly we're using the response class provided by Jane addressed framework.
And so the really cool thing about this response class is that as we can read peaking on its definition
the response class is an H TTP response that allows its data to be rendered into arbitrary media types
which basically means that the Django this framework is going to decide which kind of response is most
appropriate based on the context of the request itself.
So let's continue now leave request that method equals post in that case first of all we initialize
R C realize that so serialize that equals article sterilizer to which we pass data equals request not
data and be careful because the request object used by Django its framework is different from the one
that it's used by Django.
So we can now check if serialize the that is valid which as we've said is a check that we must perform.
In this case we call C that I said don't save.
Which is then going to call specific method based on the kind of request.
And then once we called safe we could return a response.
So return response and the response is going to contain C realize that data and statues equals start
to start a GDP to a one created if C realize that dot is valid the returns false.
Instead we do return response C realize that not errors and is a status code return status dot GDP 400
by the request.
So our view is basically ready to accept both get and post requests.
We just need to include post within the list of accepted requests.
We can also add one line of space here.
Let's now restaurateur development server and let's go back to the browser API make another request
to the same endpoint and we now see data also allows post requests and if we scroll down to page we
see that we've now got a very convenient form that we can use to send data to our API basically to create
new instances.
So let's make a test.
I'm going to copy this Jason and I'm going to remove created it and updated it.
Which is we know that managed by Django.
And I'm also going to remove a deep field like so I can also change the publication date.
So let's say I want to I want this article to be published on the 25th and I'm going to change the title
first article V addressed API I can click post to make a post request to d end point that we've defined
in order to actually get a new instance.
So let's write perfect success a EDP 2 or 1 created and yet we get back add a new instance.
Now if we make a get request once again to the same endpoint we see now we've got 3 article instances
perfect.
And if we now go back to these well pseudocode yet indeterminate we see we got a little surprise see
we got a dictionary we have the validated data for example publication date includes date time dot data
you see and that's because remember India creates method within our article see that is it.
I had a defined D print print and validated data.
Therefore this print statement has been executed right before creating a new article is this that is
then been returned.