Django Url Mapping
TUTORIAL PART 4
A R U N A R U N I S T O
Now that we have a working view as
explained in the previous tutorial. We want
to access that view via a URL.
Django has his own way for URL mapping
and it's done by editing your project urls.py
file (<project_folder>/urls.py).
Thr urls.py file will look like this
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
There’s two ways to map urls in Django
framework. One you can declare it on
project folder urls file. Other one is you can
map it on your application then it configure
on project. The second way is the proper
way it will maintain your code readable and
reusable. Here, we’re going to do the second.
For that first you need to create a file named
url.py on your app folder.
After creating file import the necessary
package and set urlpattern for url mapping.
Doing all these steps you’re
(<app_folder>/urls.py) file will look like this
from django.urls import path
urlpatterns = [
Next you need to map your views with
urls.py like below
from django.urls import path
from .views import sample_view
urlpatterns = [
path('', sample_view, name="sample-view")
]
path('', sample_view, name="sample-view"):
This line defines a URL pattern.
'': The first argument is the URL pattern
itself. In this case, it is an empty string '',
which means this pattern will match the root
URL of the app (e.g.,
http://yourdomain.com/).
sample_view: The second argument is the
view function that will handle the logic for
this URL pattern. In this case, it refers to the
sample_view function imported from the
views module.
name="sample-view": The name parameter
provides a unique identifier for this URL
pattern, which can be used in Django
templates or other parts of the code to
reference this specific URL.
After successfully mapping url on your app
file you need to configure it on your project
urls.py file like below
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('sample_app.urls'))
]
The include function is used to include URL
patterns from other apps.
Now you can run the app and if you click on
the link you will navigate to templates
mapped in you’re views.py
Now we know how to map URLs, next, we will
see how to send parameters to views
through URL.
For this first we are going to create a view on
our app_folders views.py file then we will
map it with url on app_folder urls.py file. So,
first add view like below:
#<app_folder>/views.py
from django.shortcuts import render
# Create your views here.
def sample_view(request):
return render(request, "hello.html")
#sending parameter
def view_parameter(request, name):
return render(request, "hello.html", {"name":name})
After the changes in views.py goto you’re
urls.py file and map the url like below:
#<app_folder>/urls.py
from django.urls import path
from .views import sample_view, view_parameter
urlpatterns = [
path('', sample_view, name="sample-view"),
path('view_parameter/<str:name>/',
view_parameter, name="view-parameter"),
]
Now we need to add it to our template we
will discuss it on next part don’t worry. The
final results will be like below
Now, when a user visits a URL like
/view_parameter/some_value/, Django will
capture the value after /view_parameter/ as
the name parameter and pass it to the
view_parameter function.
Like our examples above:
1. If the user visits ‘/view_parameter/Arun/’
the view will recieve name=”Arun”
2. The user visits ‘/view_parameter/Arun
Arunisto/’, the view will recieve
name=”Arun Arunisto”.
3. The user visits
‘/view_parameter/SendingParameter/’,
the view will recieve
name=”SendingParameter”.