Django URLs and Views

Table of contents

Django stands as a powerhouse web framework for Python, empowering developers to craft robust and scalable web applications. Django's URL routing system is like a map that guides incoming web requests to the right place in your Django application. In this post, we'll look into the inner workings of Django's URL handling and views.

URL Routing

In Django, URLs find their definition in the urls.py file, housing a collection of URL patterns that align with views. Each URL pattern, crafted as a regular expression, attempts to match a URL. When a user asks for a URL, Django traverses the roster of URL patterns listed in urls.py, selecting the first pattern that aligns with the URL. If pattern emerge, Django issues a 404 error.

The urls.py file in Django serves as a bridge between URLs and view functions. A URL is simply the web address that users visit in their browsers, like /about/ or /contact/. A view function is a Python function that handles what happens when a user visits a particular URL. It receives a request from the user's browser and returns a response, usually in the form of a webpage.

So, in simpler terms, urls.py helps us match each web address a user might visit to the right piece of code that will show them the appropriate webpage.

Consider the following urls.py file:

from django.urls import path
from django.contrib import admin
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('about/', views.about),
    path('', views.homepage),
]

from django.urls import path: This line imports the path function from the django.urls module. The path function is used to define URL patterns in Django.

from django.contrib import admin: This line imports the admin module from django.contrib, which is used to set up the Django admin interface.

from . import views: This line imports the views module from the current directory (denoted by .). The views module contains the view functions referenced in the URL patterns.

URL Patterns:

path('admin/', admin.site.urls): This line defines a URL pattern for accessing the Django admin site. When a user visits a URL that starts with admin/, Django routes the request to the Django admin site. The admin.site.urls attribute provides the URL patterns for the Django admin interface.

path('about/', views.about): This line defines a URL pattern for the "about" page. When a user visits the URL about/, Django calls the about view function from the views.py file to handle the request.

path('', views.homepage): This line defines the URL pattern for the homepage of the website. When a user visits the root URL ('/'), Django calls the homepage view function from the views.py file to handle the request.

View

When a user visits a URL it figures out which URL a user is trying to access, it calls a specific function called a "view" to handle that request. This view function takes in the request made by the user and then decides what content to show back to the user.

So, in simple terms, a view function in Django is like the host of a party, receiving requests from users and deciding what responses to send back. These responses could be simple text messages, complete webpages, or even structured data in JSON format.

Consider the following views.py file:

from django.http import HttpResponse

def homepage(request):
    return HttpResponse('homepage')

def about(request):
    return HttpResponse('about')
  1. from django.http import HttpResponse: This imports the HttpResponse class from the django.http module, which is used to generate HTTP responses.

  2. def homepage(request): This defines a view function named homepage that takes a request object as its parameter. Inside the function, it returns an HttpResponse object with the content 'homepage'. This view function represents the homepage of the website.

  3. def about(request): This defines another view function named , that also takes a request object as its parameter. Inside the function, it returns an HttpResponse object with the content 'about'. This view function represents the about page of the website.

In summary, the urls.py file defines URL patterns and maps them to corresponding view functions from the views.py module. The views.py file contains the view functions that handle HTTP requests and generate HTTP responses for different URLs.