WEBVTT
Kind: captions
Language: en

00:00:00.030 --> 00:00:05.190
Now that we have our API view we can go
ahead and wire this up to a URL in

00:00:05.190 --> 00:00:11.849
Django the way that Django urls work is
that you have a urls dot py file in the

00:00:11.849 --> 00:00:16.980
root of our project directory this is
the entry point for all of the URLs in

00:00:16.980 --> 00:00:22.289
our app you can see here that we have
one item already in our URLs pattern and

00:00:22.289 --> 00:00:28.180
this is the item which links
up the admin URL to the admin app

00:00:28.180 --> 00:00:33.480
This is created and enabled by default when
you first create a Django project so

00:00:33.480 --> 00:00:38.370
when you head over to admin or forward
slash admin on our project like we did

00:00:38.370 --> 00:00:44.879
with our admin site here it basically
looks up our URLs dot py file and it

00:00:44.879 --> 00:00:50.250
matches it with the first URL that it
finds so when you put an admin forward

00:00:50.250 --> 00:00:55.680
slash it detects this and then it passes
in the URLs for the admin site which

00:00:55.680 --> 00:01:01.460
connects that URL to the admin app
that's included with Django by default

00:01:01.460 --> 00:01:08.729
We're going to create a new URLs dot py file for
our profiles API app so let's go ahead

00:01:08.729 --> 00:01:15.960
and create a new file in our profiles
API app called URLs dot py this is where the

00:01:15.960 --> 00:01:22.409
URLs for our API are going to be stored
so first what we need to do is open up

00:01:22.409 --> 00:01:28.590
the project's URL file again and we need
to include our URLs dot py file in the

00:01:28.590 --> 00:01:34.860
URL patterns list of the root project
URLs so we're going to start by adding a

00:01:34.860 --> 00:01:39.630
new import to the top here next to URLs
and we're going to add one called

00:01:39.630 --> 00:01:41.200
include

00:01:41.200 --> 00:01:45.119
Include is a function that you
can use to include URLs from other apps

00:01:45.119 --> 00:01:49.040
in the root project URLs file

00:01:49.040 --> 00:01:53.579
So below
this admin line let's create a new line

00:01:53.579 --> 00:02:00.620
and let's write path API forward slash

00:02:00.620 --> 00:02:03.899
and then we'll call include and we'll

00:02:03.899 --> 00:02:08.759
pass in the name of the app and the
module we want to include that contains

00:02:08.759 --> 00:02:13.860
our urls so we're going to type profiles
underscore API

00:02:13.860 --> 00:02:18.720
because that's the name of our app here
and the module that contains the URLs is

00:02:18.720 --> 00:02:24.860
just called URLs so do dot URLs

00:02:24.860 --> 00:02:27.150
Okay make sure you save that file and

00:02:27.150 --> 00:02:31.920
I'll just talk through how this works so
what happens is when you go to forward

00:02:31.920 --> 00:02:39.990
slash api in the web server it will pass
in the request to our django app which

00:02:39.990 --> 00:02:44.760
will then look up the URL patterns for
the first URL which matches the URL that

00:02:44.760 --> 00:02:51.630
we've entered it will then pass in all
of the URLs that match with the API URL

00:02:51.630 --> 00:02:58.260
and then it will load up the sub URLs
in our urls.py so we're going to

00:02:58.260 --> 00:03:06.150
basically add the api forward slash as the
prefix of the urls for our api

00:03:06.150 --> 00:03:12.990
app so anything after the api gets
passed into the next urls file so let's

00:03:12.990 --> 00:03:17.820
open up the new URLs file that we have in
our profiles API and let's start by

00:03:17.820 --> 00:03:25.320
importing the path function so from
Django dot URLs import path and then we'll

00:03:25.320 --> 00:03:32.930
also import our view or our views module
which contains our API view so do from

00:03:32.930 --> 00:03:39.140
profiles underscore API import views

00:03:39.140 --> 00:03:42.000
Now
we need to create a new variable or a

00:03:42.000 --> 00:03:48.959
new list variable called URL patterns
and just like we have in our

00:03:48.959 --> 00:03:54.030
project URLs file we're going to create
the list of paths that map to views in

00:03:54.030 --> 00:04:02.190
our project so we're only going to create one
and it's going to be path hello - view

00:04:02.190 --> 00:04:07.440
that's going to be the name of our URL
and then we're going to map it to the views

00:04:07.440 --> 00:04:18.450
dot hello API view and then we're going to
call as view on our API view class okay

00:04:18.450 --> 00:04:24.120
so what will happen here is make sure
you save this file and when we load up

00:04:24.120 --> 00:04:27.570
our project it's going to first check the
URLs for

00:04:27.570 --> 00:04:34.020
the matching base URL API so then it's
going to pass anything past API so

00:04:34.020 --> 00:04:38.550
anything on the right-hand side of the
slash it's going to pass through and try

00:04:38.550 --> 00:04:41.500
and match it into the next URLs dot py

00:04:41.500 --> 00:04:44.280
so when you type in our webserver

00:04:44.280 --> 00:04:50.430
forward slash API it takes you to this
URLs file and then it will match hello

00:04:50.430 --> 00:04:57.000
view so the URL that is going to map to
our view is the web server address

00:04:57.000 --> 00:05:03.160
forward slash API forward slash hello view

00:05:03.160 --> 00:05:04.770
when django matches this URL

00:05:04.770 --> 00:05:11.520
it will then call the views dot hello
api view dot as view which is the

00:05:11.520 --> 00:05:19.320
standard function that we call to
convert our api view class to

00:05:19.320 --> 00:05:25.020
be rendered by our urls so basically
Django rest framework will call this get

00:05:25.020 --> 00:05:31.880
function if a HTTP GET request is made
to our URL

00:05:31.880 --> 00:05:33.780
Okay so that's how you map a

00:05:33.780 --> 00:05:38.090
URL to an API view in Django

