001 What are URLs & Views

In networking, a router looks at the destination IP or prefix and decides where to send the packet.  In Django, the URL dispatcher looks at the requested path (e.g. /customer/123) and decides which view function should handle it.

On the other hand, view handles the request and sends a response (e.g. Network service or process). View in Django is a function or class that receives a request (like a data packet hitting a destination port) and returns a response (like an ACK). 

 

002 Creating URL & View

# Views will handle the incoming request and will return this hello world response. 
 
{APP:post}
from django.http import HttpResponse
 
# Function
def helloWorld(request):
return HttpResponse(“Hello World!”)
 
 
# By default we have URL patterns variable and it is a list. 
# We create the URL inside the URL variable urlpatterns.
 
{APP:default}
#import view: [posts = app]
from posts import views
 
 
urlpatterns = [ #Default
    path(‘admin/’, admin.site.urls), #Default
    path(‘post/helloworld/’,views.helloWorld)
]
 
!
! Page not found error 404 and because we are not handling the root URL.
! What will happen when user will type the domain of our website
!
 
# Now instead of writing our URL in the default app, we are creating it inside the new app. 
# Create a new URL on your new created app.
 
# So we  have to import that path fuction to create URL. 
from django.urls import path
from . import views
 
# Manually create the urlpattern. 
urlpatterns = [
    path(‘post/helloworld/’,views.helloWorld)
]
 
!
! Page not found error 404 because when we requested this URL post slash hello world ! Django tried to seach for this URL.
!
! When user request post/helloworld/, Django tried to search for this URL inside the ! project default URL.py.
!
! As we didn’t define the URL inside the project, django was not able to find it and ! it returned a error.
!
 
# import include
 
# Solution: Create a URL inside the project file as well 
urlpatterns = [ #Default
    path(‘admin/’, admin.site.urls), #Default
    path(‘post/’, include(‘posts.urls’)) # We are telling Django that whenever user will request post/, go to the posts app url.py
]

003 Behind the scene

Flow:

In our case, Instead of defining all the URLs inside the project, we created another urls.py inside the App.

One reason is that as our Django project will grow, we will have different apps inside the Django and for each app we will have different URLS. 

  • path(‘admin/’, admin.site.urls) – When someone visits https://127.0.0.1:8000/admin/. Django routes the request to its build-in admin interface.
  • path(“”, include(“myapp.urls”)) – Meaning – For any other request (that doesn’t start with admin/), check the routes defined in myapp/urls.py.  Default route in networking? 🙂

  •  path(“”, views.home, name=”home”) – Handles the root URL of your app. It calls the home () function in your views.py.
  • path(“todos/”, views.todos, name=”Todos”) – Handles requests that go to http://127.0.0.1:8000/todos/. It calls the todos() function in your view.py

  • Home() function loads the home.html termplate and sends it back to the browser.
  • Todo() function retrieves all records from the TodoItem table in your database.

004 Dynamic URL

Just like in networking where we have static and dynamic routing, Django also uses static and dynamic URLs — and the concept is quite similar.

A static URL always points to the same destination, such as your homepage, dashboard, or help page. It doesn’t change regardless of who accesses it.

On the other hand, a dynamic URL adjusts based on parameters or variables — for example, an ID or slug — allowing it to serve different content for different requests.

Examples:

  1. youtube.com → Homepage — the same for everyone.

  2. youtube.com/watch?v=123abc → Dynamic page — displays a specific video based on its content ID.

 

Django URLs & Views

001 What are URLs & Views

In networking, a router looks at the destination IP or prefix and decides where to send the packet.  In Django, the URL dispatcher looks at the requested path (e.g. /customer/123) and decides which view function should handle it.

On the other hand, view handles the request and sends a response (e.g. Network service or process). View in Django is a function or class that receives a request (like a data packet hitting a destination port) and returns a response (like an ACK).

 

002 Creating URL & View

# Views will handle the incoming request and will return this hello world response.
{APP:post}
from django.http import HttpResponse
# Function
def helloWorld(request):
return HttpResponse(“Hello World!”)
# By default we have URL patterns variable and it is a list.
# We create the URL inside the URL variable urlpatterns.
{APP:default}
#import view: [posts = app]
from posts import views
urlpatterns = [ #Default
    path(‘admin/’, admin.site.urls), #Default
    path(‘post/helloworld/’,views.helloWorld)
]
!
! Page not found error 404 and because we are not handling the root URL.
! What will happen when user will type the domain of our website
!
# Now instead of writing our URL in the default app, we are creating it inside the new app.
# Create a new URL on your new created app.
# So we  have to import that path fuction to create URL.
from django.urls import path
from . import views
# Manually create the urlpattern.
urlpatterns = [
    path(‘post/helloworld/’,views.helloWorld)
]
!
! Page not found error 404 because when we requested this URL post slash hello world ! Django tried to seach for this URL.
!
! When user request post/helloworld/, Django tried to search for this URL inside the ! project default URL.py.
!
! As we didn’t define the URL inside the project, django was not able to find it and ! it returned a error.
!
# import include
# Solution: Create a URL inside the project file as well
urlpatterns = [ #Default
    path(‘admin/’, admin.site.urls), #Default
    path(‘post/’, include(‘posts.urls’)) # We are telling Django that whenever user will request post/, go to the posts app url.py
]
003 Behind the scene

Flow:

In our case, Instead of defining all the URLs inside the project, we created another urls.py inside the App.

One reason is that as our Django project will grow, we will have different apps inside the Django and for each app we will have different URLS.

  • path(‘admin/’, admin.site.urls) – When someone visits https://127.0.0.1:8000/admin/. Django routes the request to its build-in admin interface.
  • path(“”, include(“myapp.urls”)) – Meaning – For any other request (that doesn’t start with admin/), check the routes defined in myapp/urls.py.  Default route in networking? 🙂
  •  path(“”, views.home, name=”home”) – Handles the root URL of your app. It calls the home () function in your views.py.
  • path(“todos/”, views.todos, name=”Todos”) – Handles requests that go to http://127.0.0.1:8000/todos/. It calls the todos() function in your view.py
  • Home() function loads the home.html termplate and sends it back to the browser.
  • Todo() function retrieves all records from the TodoItem table in your database.
004 Dynamic URL

Just like in networking where we have static and dynamic routing, Django also uses static and dynamic URLs — and the concept is quite similar.

A static URL always points to the same destination, such as your homepage, dashboard, or help page. It doesn’t change regardless of who accesses it.

On the other hand, a dynamic URL adjusts based on parameters or variables — for example, an ID or slug — allowing it to serve different content for different requests.

Examples:

  1. youtube.com → Homepage — the same for everyone.

  2. youtube.com/watch?v=123abc → Dynamic page — displays a specific video based on its content ID.

 

Leave a Comment

Your email address will not be published. Required fields are marked *