Now that we have our API view we can go ahead and wire this up to a URL in
Django the way that Django urls work is that you have a urls dot py file in the
root of our project directory this is the entry point for all of the URLs in
our app you can see here that we have one item already in our URLs pattern and
this is the item which links up the admin URL to the admin app
This is created and enabled by default when you first create a Django project so
when you head over to admin or forward slash admin on our project like we did
with our admin site here it basically looks up our URLs dot py file and it
matches it with the first URL that it finds so when you put an admin forward
slash it detects this and then it passes in the URLs for the admin site which
connects that URL to the admin app that's included with Django by default
We're going to create a new URLs dot py file for our profiles API app so let's go ahead
and create a new file in our profiles API app called URLs dot py this is where the
URLs for our API are going to be stored so first what we need to do is open up
the project's URL file again and we need to include our URLs dot py file in the
URL patterns list of the root project URLs so we're going to start by adding a
new import to the top here next to URLs and we're going to add one called
include
Include is a function that you can use to include URLs from other apps
in the root project URLs file
So below this admin line let's create a new line
and let's write path API forward slash
and then we'll call include and we'll
pass in the name of the app and the module we want to include that contains
our urls so we're going to type profiles underscore API
because that's the name of our app here and the module that contains the URLs is
just called URLs so do dot URLs
Okay make sure you save that file and
I'll just talk through how this works so what happens is when you go to forward
slash api in the web server it will pass in the request to our django app which
will then look up the URL patterns for the first URL which matches the URL that
we've entered it will then pass in all of the URLs that match with the API URL
and then it will load up the sub URLs in our urls.py so we're going to
basically add the api forward slash as the prefix of the urls for our api
app so anything after the api gets passed into the next urls file so let's
open up the new URLs file that we have in our profiles API and let's start by
importing the path function so from Django dot URLs import path and then we'll
also import our view or our views module which contains our API view so do from
profiles underscore API import views
Now we need to create a new variable or a
new list variable called URL patterns and just like we have in our
project URLs file we're going to create the list of paths that map to views in
our project so we're only going to create one and it's going to be path hello - view
that's going to be the name of our URL and then we're going to map it to the views
dot hello API view and then we're going to call as view on our API view class okay
so what will happen here is make sure you save this file and when we load up
our project it's going to first check the URLs for
the matching base URL API so then it's going to pass anything past API so
anything on the right-hand side of the slash it's going to pass through and try
and match it into the next URLs dot py
so when you type in our webserver
forward slash API it takes you to this URLs file and then it will match hello
view so the URL that is going to map to our view is the web server address
forward slash API forward slash hello view
when django matches this URL
it will then call the views dot hello api view dot as view which is the
standard function that we call to convert our api view class to
be rendered by our urls so basically Django rest framework will call this get
function if a HTTP GET request is made to our URL
Okay so that's how you map a
URL to an API view in Django