Just like with our API view we need to register our new view set with a URL to
make it accessible through our API the way that you register the view set with
a with a URL is slightly different from how you register the API view with the
URL if you open up the URL dot py file we can see that we passed in a new URL
pattern using the path function to register our API view with the hello -
view URL
the way that view sets work is you use what's called a router which is
a class that's provided by the Django rest framework in order to generate the
different routes that are available for our view set so with our view set you
may be accessing the list request which is just the route of our API and in this
case you would use a different URL then if you are accessing a specific
object to do an update a delete or a get
I'll show you how this works in a couple
more videos when we test our view set in the browser for now let's create a
default router and register our view set with the default router and
pass the URLs into URL pattern
so before we can do this we need a couple more
imports here at the top of the file so we need to add include after the path
import this imports the function called include which comes with the Django dot
URLs module this is used for including lists of URLs in the URL pattern and
assigning the lists to a specific URL
Next we're going to import the default
router so if we do from rest underscore Framework
dot routers import default router
Okay and the way that you use default route is i
you assign the router to a variable by doing router equals default router and
then you use router dot register to register specific view sets with our
router so let's type router dot register the first argument is the name
of the URL that we wish to create so we're going to create one called hello -
view set just like we have hello - view here we're going to access our API using
hello - view set because the router will create all of the four URLs for us we
don't need to specify a forward slash here when we define our view set URL
name
the second argument is the view set that we wish to register to this URL so
if we type views dot hello view set which is the view set we
created previously in our views dot py file
finally we need to specify a base name for our view set so you do that by
typing base underscore name equals hello view set this is going to be used for
retrieving the URLs in our router if we ever need to do that using the URL
retrieving function provided by Django
Okay so now that we've registered our
router and we've created our router and registered our new view set
with it let's add this to our URLs patterns
create a new line below the existing
path that we created and type path and we're going to create a new path with
just a blank string and then the second argument we're going to use the include
function that we imported here we're going to type include router dot URLs so
as you register new routes with our router it generates a list of URLs that
are associated for our view set it figures out the URLs that are required
for all of the functions that we add to our view set and then it generates this
URLs list which we can pass in to using the path function and the include
function to our URL patterns
the reason we specify a blank string here is
because we don't want to put a prefix to this URL we just want to include all of
the URLs in the base of this URLs file
Ok so save that file and that's how you
register view sets with routers in the Django rest framework