WEBVTT
Kind: captions
Language: en

00:00:00.060 --> 00:00:06.600
Just like with our API view we need to
register our new view set with a URL to

00:00:06.600 --> 00:00:13.049
make it accessible through our API the
way that you register the view set with

00:00:13.049 --> 00:00:17.910
a with a URL is slightly different from
how you register the API view with the

00:00:17.910 --> 00:00:23.760
URL if you open up the URL dot py file we
can see that we passed in a new URL

00:00:23.760 --> 00:00:30.060
pattern using the path function to
register our API view with the hello -

00:00:30.060 --> 00:00:32.500
view URL

00:00:32.500 --> 00:00:37.200
the way that view sets work is
you use what's called a router which is

00:00:37.200 --> 00:00:42.480
a class that's provided by the Django
rest framework in order to generate the

00:00:42.480 --> 00:00:47.730
different routes that are available for
our view set so with our view set you

00:00:47.730 --> 00:00:54.270
may be accessing the list request which
is just the route of our API and in this

00:00:54.270 --> 00:00:59.489
case you would use a different URL then
if you are accessing a specific

00:00:59.489 --> 00:01:03.420
object to do an update a delete or a get

00:01:03.420 --> 00:01:06.270
I'll show you how this works in a couple

00:01:06.270 --> 00:01:11.909
more videos when we test our view set in
the browser for now let's create a

00:01:11.909 --> 00:01:16.860
default router and register our
view set with the default router and

00:01:16.860 --> 00:01:19.720
pass the URLs into URL pattern

00:01:19.720 --> 00:01:22.290
so before we
can do this we need a couple more

00:01:22.290 --> 00:01:27.530
imports here at the top of the file so
we need to add include after the path

00:01:27.530 --> 00:01:33.509
import this imports the function called
include which comes with the Django dot

00:01:33.509 --> 00:01:40.079
URLs module this is used for including
lists of URLs in the URL pattern and

00:01:40.079 --> 00:01:44.780
assigning the lists to a specific URL

00:01:44.780 --> 00:01:47.460
Next we're going to import the default

00:01:47.460 --> 00:01:52.310
router so if we do from rest underscore
Framework

00:01:52.310 --> 00:01:59.020
dot routers import default router

00:01:59.020 --> 00:02:01.860
Okay and the way that you use default route is i

00:02:01.860 --> 00:02:09.319
you assign the router to a variable by
doing router equals default router and

00:02:09.319 --> 00:02:16.020
then you use router dot register to
register specific view sets with our

00:02:16.020 --> 00:02:22.620
router so let's type router dot
register the first argument is the name

00:02:22.620 --> 00:02:27.450
of the URL that we wish to create so
we're going to create one called hello -

00:02:27.450 --> 00:02:33.060
view set just like we have hello - view
here we're going to access our API using

00:02:33.060 --> 00:02:39.150
hello - view set because the router will
create all of the four URLs for us we

00:02:39.150 --> 00:02:43.950
don't need to specify a forward slash
here when we define our view set URL

00:02:43.950 --> 00:02:45.400
name

00:02:45.400 --> 00:02:50.130
the second argument is the view set
that we wish to register to this URL so

00:02:50.130 --> 00:02:56.610
if we type views dot hello
view set which is the view set we

00:02:56.610 --> 00:03:00.560
created previously in our views dot py file

00:03:00.560 --> 00:03:06.060
finally we need to specify a base name
for our view set so you do that by

00:03:06.060 --> 00:03:14.160
typing base underscore name equals hello
view set this is going to be used for

00:03:14.160 --> 00:03:20.489
retrieving the URLs in our router if
we ever need to do that using the URL

00:03:20.489 --> 00:03:25.640
retrieving function provided by Django

00:03:25.640 --> 00:03:28.079
Okay so now that we've registered our

00:03:28.079 --> 00:03:33.060
router and we've created our
router and registered our new view set

00:03:33.060 --> 00:03:37.140
with it let's add this to our URLs patterns

00:03:37.140 --> 00:03:39.690
create a new line below the existing

00:03:39.690 --> 00:03:45.000
path that we created and type path and
we're going to create a new path with

00:03:45.000 --> 00:03:49.829
just a blank string and then the second
argument we're going to use the include

00:03:49.829 --> 00:03:58.500
function that we imported here we're
going to type include router dot URLs so

00:03:58.500 --> 00:04:04.260
as you register new routes with our
router it generates a list of URLs that

00:04:04.260 --> 00:04:09.299
are associated for our view set it
figures out the URLs that are required

00:04:09.299 --> 00:04:13.980
for all of the functions that we add to
our view set and then it generates this

00:04:13.980 --> 00:04:19.410
URLs list which we can pass in to using
the path function and the include

00:04:19.410 --> 00:04:22.420
function to our URL patterns

00:04:22.420 --> 00:04:25.889
the reason we specify a blank string here is

00:04:25.889 --> 00:04:30.389
because we don't want to put a prefix to
this URL we just want to include all of

00:04:30.389 --> 00:04:35.720
the URLs in the base of this URLs file

00:04:35.720 --> 00:04:38.580
Ok so save that file and that's how you

00:04:38.580 --> 00:04:45.020
register view sets with routers in the
Django rest framework

