Now that we've created our API view and
mapped it to a URL we can go ahead and
test our new API view in the browser. So
minimize atom editor and load up the git
bash or terminal window that is running
our profiles project vagrant server. I
still have the development server
running from the last lesson and I
received this error message from when we
were changing the files. This is saying it
cannot find a module named profiles API.URLs
and I believe we received this
error because we added
the import or the include to our urls
file before we create our urls file. This
is fine, the django development
server just need to refresh
so press control-c to cancel the server
and then press the up arrow to run the
run server command again so python
manage.py run server 0.0.0.0
port 8080 hit enter to start the server
again and this time you can see that
there's no error being displayed. Alright
so now we can head over to Chrome and we
can test our API. So if you head over to
just the root URL of the project you can
see it says page not found so this means
none of the URLs that we
entered didn't match any of the URLs
that we put in.
If you type /api to the
end and hit enter again it says again
404 not found because we don't have any
views mapped to the /API URL
However we do have a hello view URL
mapped to a view so let's try that let's
try entering /API/hello
view and hit enter
Here you can see it's rendered the
results of our API view. So it says here
hello API is the name of our API view
it's got the URL and it's received HTTP
200 ok, and here it has returned the
message and the list of attributes that
an API view supports here that we defined
in our view. Let's just look at
the code and the results side by side.
This is the beautiful thing about Django
rest framework is it renders the results in
a nice easy to navigate page so you can
view the actual API in the browser as
you're developing. When we went to /api
/HelloView django looked at the root
project's URL file. Here it found that
there was a URL that begins with API
Here so it matched this API part of the
URL. And then it knew that it should
look in the URLs file of the
profiles API so it went to the URLs
file the profiles API and it saw that
there's a part of the URL called hello
view. And it matched this to the hello view
in our URL. So then django knew that it
needed to render this particular view:
views.HelloApiView. So it looked in our
views file which comes from the root of
this folder so look in here and it found
the HelloApiView. Because we use the
HTTP GET request which is the standard
request when you enter the URL in the
browser it found this HTTP GET function
defined here. So it ran the code in this
function and then it returned this
response object. And Django rest
framework knows how to convert this
response object into a nice-looking easy
to read web page like this. So that's
what it did it took our response object
which had a message hello which is here
and the list:
an API view list
and it rendered that as a nice webpage.
So that's how you test the API view that
we've just created. Let's move on and
commit our changes to git. Minimize atom
and minimize Chrome and load up the git
bash that is on our project that is not
the vagrant server so the second one the
one that's just on our local machine on
our project. And type git add to add the
new files get add . and then type git
commit -am and we'll give it the
message "Created our first api view." and hit
enter.
And there you go the changes have been
committed. So that's how you create a
basic API view. Thank you so much for
watching and I'll see you in the next
lesson.