Before we jump right into building our
API I'm going to show you the basics of
creating an API view in Django. Start by
loading up the atom editor and opening
the views.py file which is inside our
profiles API app. The views.py file
is where we create our views for our API.
A view is basically the application
logic behind our API so it's the code
that gets ran when the user visits our
API endpoint. We're going to create a
hello world API view, a very basic API
view so I can demonstrate how they work.
We're going to start by importing the
API view and the response object from
the Django rest framework. So enter from
rest underscore framework dot views import
API view. This imports the API view class
from the rest framework dot views module. The
rest framework is the Django rest
framework. Second, we're going to import a
response object so type from rest
underscore framework dot response import
response with a capital R. It's very
important that you get the
capitalisation exact so APIView is API
V all capital and then i e w and response
is response with a capital R. A response
object is a standard response object
that we return from our API view that
can be rendered into an API output. So
that comes with the Django rest
framework and the Django rest framework
basically standardized the response that
we need to output once we process the
logic and we want to return a response.
Now that we've added the import let's
define our hello API view class.
So we're going to define a class called
hello API view and we're going to
inherit from the API view base class. So
type class hello API view and inherit
from the API view class. We're going to write
a doc string "Test API view." Because this is the
class we're going to use to test the API
view functionality. So I mentioned in the
previous lesson that API views work by
defining functions that match the
standard HTTP methods. The most common
HTTP method is HTTP GET. This is used
whenever you want to get a list or an
object from a specific API. So let's
define a function called get, so type
def get and we're going to give it the
parameters self, request, format. I'm going
to give it the doc string "Returns a list
of API view features." So what we're going
to do in this HTTP GET function for this
hello API view is we're simply going to
return a list of features that the API
view offers so we're going to create a
manual list of the features and we're
going to return that object. So let's
create a new variable
called an API view. An API view and
we're going to create a new list so put
equals open the square brackets and the
first item in the list
we're just going to explain what an API
view does. So an API view uses HTTP
methods as functions get post patch put
delete. That's the first thing we're
going to add to our list. The second
thing we're going to add to our list is
it is similar to a traditional django view
The third thing we're going to add
to our list is it gives you the most
control over your logic. And finally
we're going to write, is mapped manually
to URLs. Okay so that's a list of some
features that an API view provides. Now
we're going to return this list in a
response object so it can be output from
the API. We're going to type return
response which is this response object
that we imported up here and a response
object must be passed a dictionary to
return the response. So the response is
effectively a dictionary which is
converted to JSON which is output to the
screen. Let's create the dictionary
inside the response and we're just going
to say a message and we're going to just
write a message hello and the second
thing is we're going to write an API view
and we're going to return our an API
view variable the list. Okay so that's
how you create a hello API view in
Django. Thanks for watching, I hope you
found this useful and I'll see you in the
next lesson.