alright guys welcome back and in this
video we're going to start working on
our views and by the end of this video
you guys are going to learn how to make
your very first API request and get
actual Jason back so it's going to be
awesome so go ahead and pop open views
that py and just to save you guys some
time I actually copied all the imports
we're going to need and I'll talk to you
guys through in detail exactly whenever
we are using these what they do but just
real quick I'm you already know what
this does right here get to object or
returns a 404 if the object doesn't
exist so let's say they requested like
tuna fish company and it didn't exist on
API view is just the way that we can
make these normal views return API data
this response is so we can send back a
specific response status and it kind of
works with this so basically before we
were just sending back HTML now we need
to send back Jason and also whenever you
just request the page and it returns
that normally that is called a 200
response everything went fine
um you guys already know what a 404 is
that means hey you were you requested
something but it doesn't exist anymore
so maybe your request is someone's
picture from Facebook and they deleted
it so that's when you would get 4 or 4
and there are other ones too like arm
400 means that the user was supposed to
be doing something they shouldn't like
it the client the server communicated
correctly but they like try to request
like the admin page or something so
anyways that's what these are going to
do and then we of course we just
imported stock all this information in
serializers all right so what I'm going
to do is I am going to set this up as a
class based view so in other words we're
going to set it up this way because we
want to have it where if the user
requests these URLs in different kinds
of ways then they handle them
differently and I'll show you guys what
I'm talking about so for this first
class I'm just going to name stock list
and it's going to inherit from API Bo
now what I was talking about I'm going
to go ahead and make
two methods in here so the first one is
whenever they make a get request and the
second one is whenever they make a post
request so for this example I'm going to
have it where whenever they make a get
request for stock list it's just going
to return all of the stocks that we have
a list of all pretty much everything in
our database now whenever they make a
post request what's going to happen is
I'm going to let them create a new stock
so again a post is kind of like
submitting a form and get is just kind
of like reading information so say that
this either lists all stocks or create
the new one now before I continue I want
to mention this would you actually want
to set this up this way
would you actually want to put your
create functionality into a separate
class named stock great well you may
want to but then how am I going to
explain class-based REST API abuse oh
and actually above all of these classes
I'm just going to write a little demo
URL so again whenever they type this URL
in our website followed by stocks it's
going to get all the stocks and then
later on in the next video I'm going to
show you guys how to just get
information about one stock using this
URL or like Amazon or something like
that but for right now I just keep
things simple all right let me give
myself a little bit more space to scroll
around all right so for this get method
again this is the one that's going to
return all of the stock information so
we need to pass in request and instead
of pass which pretty much means do
nothing the first thing we're going to
do is we're pretty much just going to go
in our database and get all of the
stocks so I'll just say stock objects
all simple as that nothing new so once
we have all of our stock objects all of
these right here we need to go ahead in
serialize them in other words convert it
to Jason
so I'm just going to say serializer
equals stock serializer and this takes
two parameters the first one is what
objects are you trying to serialize well
all those stocks that we just got and
the second one is we need to specify
that there are many of them so it
doesn't just try to return one JSON
object and after this of course remember
every single View function what it needs
to do is it needs to return an HTTP
response so this is going to have JSON
in it but still it's HTTP responses
being sent over the Internet so we just
return a response and what is the
information in our response well just at
serializer data in other words just the
JSON data simple enough and ladies and
gentlemen that is it this URL is almost
working the one other thing that we need
to do is we just need to specify in URLs
right here and we actually need to
import some other stuff I forgot just
make sure that these URLs are compatible
for basically an API so a rest framework
where you add URL patterns imports
format suffix patterns and also from
that view we just made we actually need
to import it so from companies import
views
all right so URL patterns format suffix
pattern 0 patterns and I'll explain
later on it once we create a bunch more
like for updating information and
deleting it then I'll explain that but
for right now I just kind of want to get
things started
so we'll keep the admin since that's the
proper URL for that still but now we
need to add another one for stocks so
again you remember the structure we
defined right here so when they go to
admin it's going to take them to the
admin panel whenever they go to stocks
it's just going to return a list of
stocks so instead of this we can
actually keep things real simple and say
views stock list as view so again what
it's going to do is it's going to say
hey go ahead and we get rid of this go
in your views to the class stock list
and return these as a view simple enough
so now check this out set up our admin
panel we can go to stocks and boom look
at that and again you can actually just
do this over Python or you can do it
with your iPhone or Android basically
whenever you throw in this URL you can
connect to your website's database and
get information from it on any device in
the world so I'm going to do with the
browser right now but actually check
this out so this is a program that I
have and it's called postman and
basically what you can do is you can
just send different kinds of requests
like post request put patch delete and
I'm just going to make a simple get
request from here send it and check it
out so this would you know kind of more
clearly resemble using this information
in a program like a piece of software or
something like that so we now can
connect to our database get the data and
then we can either analyze it or do
whatever the heck we want with it so
that is the basics of how to make a
simple get request and in the next
videos I'll show you guys how to add
new stocks using the simple request and
also how you can just retrieve one stock
how you can edit stocks that are already
in there how you can delete them how to
have admin permissions so you just don't
have everyone deleting them a bunch of
crap it's going to be awesome
I'm excited I'll see you next time