Now in your app playground folder, create a new file called “urls.py”. This is where we will map our URLs to our views functions.
On top of the “urls.py” file import the “path” function from “django.urls”. And also import the views module from the current folder.
Now create a variable called “urlpatterns” and set it to a list of urlpattern objects. Now use the path function to create a urlpattern object.
Inside the path function, you specify two parameters. One is for the route and the second one is for the view function you want to map to the route. See below
from django.urls import pathfrom . import views
# this is called a url configurationurlpatterns = [ path("playground/hello", views.say_hello)]Now you need to import this url configuration into the main URL configuration for the project. There is a urls.py file in your main project folder.
Inside the main urls.py file, import “include and path” from “django.urls”. And then add a URL to the urlpatterns list.
In the urlpatterns list, you also need to call the path function which also takes 2 parameters. The first one is a route, and the second one is where you want django to look for the route(the app).
In our case, our route is ‘playground/’ and second parameter is “include(‘playground.urls’)”. So now django can tell that every route starting with “playground/” will be looked for in the playground.urls file. See below
from django.contrib import adminfrom django.urls import path, include
urlpatterns = [ path('admin/', admin.site.urls), path("playground/", include("playground.urls"))]Now that we have done this in our main project’s urls file. We can go to our app’s urls file and delete the “playground/” that comes before the route.
When we navigate to “playground/hello” we will be able to see “Hello World”