Now that we've created our hello API
view we need to map a URL to this view
so we can access it via HTTP server. The
way that you map URLs to views in Django
is you use something called the URL
dispatcher. The URLs are described in the
file called URLs.py. Currently if you open
up our profiles project and you look at
URLs.py you'll see the existing URL
configuration for the site. Notice here
that you have the admin site mapped to
admin.site.urls. This is for the
Django admin site which we accessed
earlier in chrome using the
/admin URL. This URL file config
tells Django to forward anything with
the admin / to the admin app.
We need to create our own mapping to our
profiles API app. We're going to call the
URL slash API so start by including the
include function at the top of our URLs.py file.
It's best practice to keep a URLs.py
file for each Python project that
you add to the app. So we're going to
create a URLs.py project here and
we're going to include it from the root
projects URLs.py file for that we
need to import the include
function. So type from django.conf.urls
import include. Okay so now we need
to add a URL to this URL patterns list.
Start by typing URL and then R and then
creating new string and start the string
with this ^ symbol and type API/
and then comma and then we want to use
the include function and pass a string
that says profiles API.URLs. Let me
just explain what this does. You describe
Django URLs using a technology called
regular expressions. Regular expressions
is a way to manipulate strings so that
you can detect certain string patterns
and extract certain data from a string.
It can get quite complicated but you
don't need to know it in too much detail
in order to make basic Django URLs. I'll
explain what this regular expression
does. So the R indicates that this string
is a regular expression. The ^ at the
beginning of the URL says we want to
ensure that the beginning
of the string starts with
API/ so anything that
starts with API/ is going
to get matched and passed through to this
profiles API.URLs file. If you want to
learn more about Django URLs I've
included a link in the resources of this
video to the Django URL dispatcher
documentation. You can head over to that
if you want to learn Django URLs in much
more detail. So we include the profiles
API.URLs file but we haven't created
that file yet so the next step is to
create this URLs file in our
profiles API project. So save this URLs
file and right-click on the profiles API
and click new file. Call the file URLs.py
hit enter and now we created a brand new file
we're going to start by including
the URL function so from
django.conf.urls import URL and we're
also going to import our views, so the
views.py file that we modified and
added our API view to earlier.
So type from . import views. So again
the dot means import from the root
location and import our views module. Now
we can describe our URLs pattern so type
urlpatterns = and create a new list
object and we're only going to add one
item to the list and we're going to
describe it as a URL so that calls this
URL function here and again we're going
to type r
for regular expression and we're going
to write our regular expression for our
URL so we want to create a URL called
hello view that's going to map to our
API view. So use the ^ and type hello
- view/ Now after the
string put a comma and we need to tell
Django which view we want to run at this
URL. So we want to call our hello API
view so we're going to type views.HelloApiView
and we want to return
this view as a view object so type dot
as underscore view, and it's a function so
you need put these two empty
brackets here. Then you can end the line
in a comma.
Okay so save that file and this is our
urls pattern so i'll just explain what's
going to happen what we call our api
view. When we call our API view it's going to
look at the root project URLs config.
It's going to detect that we have API at
the beginning of our URL and it's going
to look inside the profiles API.URLs
module. That's here profile API.URLs
module. It's then going to check the next
part of the URL after this /API
part and it's going to see that we typed
hello view and then it's going to render
this the result of this
views API view, and it's going to return
that result to the screen. Okay so that's
how you create URLs in Django thank you
for watching I'll see you in the next
lesson.