DEV Community

Cover image for Day 66 of 100 Days Of Code — Getting Started with Django: URLs, Views, and Templates
M Saad Ahmad
M Saad Ahmad

Posted on

Day 66 of 100 Days Of Code — Getting Started with Django: URLs, Views, and Templates

Previously, I was learning Python fundamentals to strengthen my basics before moving into Django. Today, for Day 66, I got into Django properly. I built my first working Django page from scratch, and in doing so, understood the most fundamental thing about Django: how a request travels through the framework and comes back as a response. Everything in Django builds on this.


The Core Idea

When someone visits a URL in your Django app, three things happen in order:

  1. Django checks urls.py to find which view matches that URL
  2. The view runs and does whatever logic is needed
  3. The view returns a response, usually a rendered HTML template

That's the entire request/response cycle. URLs → Views → Templates.


Step 1: Create a Project

django-admin startproject mysite
cd mysite
python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

Visit http://127.0.0.1:8000/ in your browser. You'll see Django's default welcome page. That means everything is working.

The project structure looks like this:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py
Enter fullscreen mode Exit fullscreen mode

Step 2: Create an App

A Django project is made up of apps. Each app handles one part of the project. Let's create one:

python manage.py startapp core
Enter fullscreen mode Exit fullscreen mode

Now register it in settings.py:

INSTALLED_APPS = [
    ...
    'core',
]
Enter fullscreen mode Exit fullscreen mode

Without this line, Django doesn't know the app exists.


Step 3: Write Your First View

Open core/views.py and write a simple view:

from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello from Django!")
Enter fullscreen mode Exit fullscreen mode

A view is just a Python function that takes a request object and returns a response. That's it. The request object contains everything about the incoming request: the method, headers, user, and data.


Step 4: Connect the View to a URL

First, create a urls.py file inside the core app:

# core/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
]
Enter fullscreen mode Exit fullscreen mode

Then include it in the project's main urls.py:

# mysite/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('core.urls')),
]
Enter fullscreen mode Exit fullscreen mode

Now visit http://127.0.0.1:8000/; you'll see "Hello from Django!" in the browser. Your first working view.


Step 5: Create a Template

Returning HTML directly from HttpResponse works, but isn't practical for real pages. Templates let you write proper HTML in separate files and render them from your view.

Create this folder structure inside your app:

core/
    templates/
        core/
            home.html
Enter fullscreen mode Exit fullscreen mode
<!-- core/templates/core/home.html -->
<!DOCTYPE html>
<html>
<head>
    <title>My Django App</title>
</head>
<body>
    <h1>Welcome to my Django app</h1>
    <p>This is rendered from a template.</p>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Now update the view to render the template instead:

from django.shortcuts import render

def home(request):
    return render(request, 'core/home.html')
Enter fullscreen mode Exit fullscreen mode

render() takes the request, the template path, and an optional context dictionary. It returns an HttpResponse with the rendered HTML.


Step 6: Pass Data to the Template

The real power of templates is displaying dynamic data. You pass data from the view through a context dictionary, then use it in the template with {{ }} syntax.

# core/views.py
def home(request):
    context = {
        'name': 'Haris',
        'day': 66,
        'topics': ['URLs', 'Views', 'Templates']
    }
    return render(request, 'core/home.html', context)
Enter fullscreen mode Exit fullscreen mode
<!-- core/templates/core/home.html -->
<!DOCTYPE html>
<html>
<head>
    <title>Day {{ day }}</title>
</head>
<body>
    <h1>Hello, {{ name }}</h1>
    <p>Today is day {{ day }} of 100 Days of Code.</p>
    <h2>Topics covered today:</h2>
    <ul>
        {% for topic in topics %}
            <li>{{ topic }}</li>
        {% endfor %}
    </ul>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Two template syntaxes to know:

  • {{ variable }} — outputs a value
  • {% tag %} — logic like loops and conditionals

Reload the browser. The page now shows dynamic data coming from the view.


How It All Connects

Here's the full picture of what happens when someone visits your homepage:

Browser requests /
        ↓
mysite/urls.py — matches '' → includes core/urls.py
        ↓
core/urls.py — matches '' → calls views.home
        ↓
views.home — builds context, calls render()
        ↓
render() — loads home.html, injects context
        ↓
HTML response sent back to browser
Enter fullscreen mode Exit fullscreen mode

Every single page in a Django app follows this exact flow. URLs route the request, views handle the logic, and templates render the output.


Django Template Language Basics

A few more things in the template language worth knowing right away:

Conditionals:

{% if day > 50 %}
    <p>More than halfway there!</p>
{% else %}
    <p>Keep going!</p>
{% endif %}
Enter fullscreen mode Exit fullscreen mode

Filters — modifying variables inline:

{{ name|upper }}         <!-- HARIS -->
{{ name|lower }}         <!-- haris -->
{{ topics|length }}      <!-- 3 -->
Enter fullscreen mode Exit fullscreen mode

Template Inheritance — the real power:

Instead of repeating the same HTML structure across every page, Django lets you define a base template and extend it.

<!-- core/templates/core/base.html -->
<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}My Site{% endblock %}</title>
</head>
<body>
    <nav>My Nav Bar</nav>
    {% block content %}{% endblock %}
</body>
</html>
Enter fullscreen mode Exit fullscreen mode
<!-- core/templates/core/home.html -->
{% extends 'core/base.html' %}

{% block title %}Home{% endblock %}

{% block content %}
    <h1>Hello, {{ name }}</h1>
{% endblock %}
Enter fullscreen mode Exit fullscreen mode

{% extends %} tells Django this template inherits from another. {% block %} defines sections that child templates can override. This keeps your HTML DRY.


Wrapping Up

Today was the real start of Django. One project, one app, one working page, but through it, I saw the full request/response cycle working end to end. URLs route to views, views pass data to templates, and templates render HTML back to the browser.

This is the foundation that everything else in Django sits on. Models, forms, authentication — all of it eventually flows through a view and out through a template.

Tomorrow: Django Models and the ORM: connecting to a database.

Thanks for reading. Feel free to share your thoughts!

Top comments (0)