Django Tutorial #2: URLs, Models, Views and Templates

Please note that this post may contain affiliate links, and for every purchase you make, at no extra cost to you, a commission fee will be rewarded to me.

In this section, we’ll talk about URLs, models, views and templates, as well as their relationships with each other.

Table of Contents


As the name suggests, the handles the URLs. It reads information from a URL and returns a view. Do not confuse this view with the view in Laravel. They are two different things, and we’ll talk about this later on.


from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('blog/', include('blog.urls'))

Line 1 to line 4 import the necessary packages.

Line 7 is for the admin panel which is included in Django. We’ll talk about this later in this tutorial.

Line 8 means if the URL starts with, Django will go to /blog/ and reads information from the rest of the URL. This functionality helps us manage the URLs for different apps.


from django.urls import path
from blog import views

urlpatterns = [
    path('', views.home, name='home'),

Line 5, if nothing follows the URL, the home view is called.


Views act like the controllers in Laravel. It is the place where you organize the logic and the behaviour of your app. Views receive requests from URLs, retrieve data from the database through models, and finally return a template.

In Django, all the views are defined in the You can define the views in classes or functions. In this tutorial, we’ll use functions since it is easier to understand.


from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello, world. You're at the home page.")

Start the server,

python runserver

Open your browser and go to

Of course, views can do a lot more than just return a string. Its primary purpose is to retrieve data from the database and manipulate them in the way you wanted and finally pass them onto a template.


Templates are what you see in your browser, they are like the views in Laravel. Which is why they are all HTML files. As an example, we create a template named home.html.

Create a new blog folder inside templates, and then create a home.html inside blog.


<!DOCTYPE html>

    <h1>My First Heading</h1>

    <p>My first paragraph.</p>


Now we can make our home view return the home.html template instead of a string.


from django.shortcuts import render

def home(request):

    return render(request, 'blog/home.html')

Refresh the browser, you should see an HTML page instead of just a string.


Remember I said views are in charge of retrieving data from the database? Well, that’s where models come in.

Each database table has a corresponding “Model”, which is defined in class, and that model will handle all the interactions with the table.

However, before we create models and templates, we need to design our database structure.

Next Post: Django Tutorial #3: Database


Leave a Reply

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