Django: URLs, views and templates – home page and generator

Lesson 4 of 9 · Python web: Django project

The frontend from the HTML, CSS and JavaScript course now becomes part of Django: the home page and the generator get addresses, Python functions that display them and templates that share a common menu and footer. CSS, JavaScript and images become static files.

Before you start: prerequisites – settings.py (SR), the frontend home page.
How to learn: work in your own project step by step and check after every lesson that the server runs; do the task at the end on your own first. The code is for Django 5.2 (LTS) and Python 3.12+; put each part into the file named in the comment at the top of the example.

Addresses (urls.py)

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

urlpatterns = [
    path("admin/", admin.site.urls),
    path("nalozi/", include("django.contrib.auth.urls")),   # login, logout, password change (lesson 7)
    path("nalozi/", include("nalozi.urls")),                # registration (lesson 6)
    path("", include("logoi.urls")),                        # naslovna, generator, moji logoi
]
# logoi/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path("", views.naslovna, name="naslovna"),
    path("generator/", views.generator, name="generator"),
    path("moji-logoi/", views.moji_logoi, name="moji_logoi"),            # lesson 8 (you can leave it out for now)
    path("moji-logoi/<int:pk>/obrisi/", views.obrisi, name="obrisi"),   # lesson 8
]

path("generator/", views.generator, name="generator") means: the address /generator/ calls the function generator, and in templates the route is referred to by name – {% url 'generator' %}. If you change the address later, the links change by themselves. The lines for nalozi are needed only in lessons 6–7 – until the file nalozi/urls.py exists, comment them out (#).

Views (views.py)

# logoi/views.py (lesson 4 – only displaying pages)
from django.shortcuts import render


def naslovna(request):
    return render(request, "logoi/naslovna.html")


def generator(request):
    naziv = request.GET.get("naziv", "Logo")[:20]          # ?naziv=... from the home page
    return render(request, "logoi/generator.html", {"naziv": naziv})

A view receives the request (method, parameters, user…) and returns a response. The function render fills a template with data from a dictionary (the context) and returns HTML. The parameter from the address ?naziv=... is read from request.GET, like URLSearchParams in JavaScript.

The shared template base.html

The menu and the footer are the same on all pages, so they are written once – in templates/base.html. The other pages inherit it and fill in only the blocks:

{% load static %}
<!DOCTYPE html>
<html lang="sr">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>{% block naslov %}Logo Kreator{% endblock %}</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
  <link rel="stylesheet" href="{% static 'logoi/css/main.css' %}">
</head>
<body>
  <nav class="navbar navbar-expand-md bg-dark" data-bs-theme="dark">
    <div class="container">
      <a class="navbar-brand fw-bold" href="{% url 'naslovna' %}">LOGO KREATOR</a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#meni" aria-label="Open the menu">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="meni">
        <ul class="navbar-nav ms-auto align-items-md-center">
          <li class="nav-item"><a class="nav-link" href="{% url 'generator' %}">Generator</a></li>
          {% if user.is_authenticated %}
            <li class="nav-item"><a class="nav-link" href="{% url 'moji_logoi' %}">My logos</a></li>
            <li class="nav-item">
              <form method="post" action="{% url 'logout' %}" class="d-inline">
                {% csrf_token %}
                <button class="btn btn-link nav-link">Odjava ({{ user.username }})</button>
              </form>
            </li>
          {% else %}
            <li class="nav-item"><a class="nav-link" href="{% url 'login' %}">Prijava</a></li>
            <li class="nav-item"><a class="btn btn-danger btn-sm ms-md-2" href="{% url 'registracija' %}">Registracija</a></li>
          {% endif %}
        </ul>
      </div>
    </div>
  </nav>

  {% if messages %}
    <div class="container mt-3">
      {% for poruka in messages %}
        <div class="alert alert-{% if poruka.tags == 'error' %}danger{% else %}{{ poruka.tags }}{% endif %}">{{ poruka }}</div>
      {% endfor %}
    </div>
  {% endif %}

  {% block sadrzaj %}{% endblock %}

  <footer class="podnozje text-white mt-5">
    <div class="container py-3 text-center small">&copy; 2026 Logo Kreator · Django projekat</div>
  </footer>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
  {% block skripte %}{% endblock %}
</body>
</html>
TagMeaning
{% load static %} + {% static '...' %}the correct address of a static file (/static/logoi/css/main.css)
{% url 'naslovna' %}the address of a route by its name
{% block sadrzaj %}{% endblock %}a place filled in by the page that inherits
{% if user.is_authenticated %}a different menu for signed-in and anonymous users (lesson 7)
{{ user.username }}outputs a value – Django automatically escapes the text, so the XSS attack from the security lesson does not get through
{% csrf_token %}protects a form from CSRF attacks – required in every POST form

The parts that use signing in (logout, login, registracija) will start working in lessons 6–7; until then you can temporarily remove them from the menu.

The home page as a template

{% extends "base.html" %}
{% load static %}

{% block naslov %}Logo Creator – make your own logo{% endblock %}

{% block sadrzaj %}
<header class="hero text-center text-white">
  <div class="container py-5">
    <h1 class="display-4 fw-bold">Logo Kreator</h1>
    <p class="lead mb-4">Make a simple logo in a minute – for free.</p>
    <form class="row g-2 justify-content-center" action="{% url 'generator' %}" method="get">
      <div class="col-12 col-sm-8 col-md-6">
        <label for="naziv" class="visually-hidden">Logo name</label>
        <input type="text" class="form-control form-control-lg" id="naziv" name="naziv"
               placeholder="Type a name, e.g. Pupin club" maxlength="20" required>
      </div>
      <div class="col-12 col-sm-auto">
        <button type="submit" class="btn btn-danger btn-lg w-100">Create a logo</button>
      </div>
    </form>
  </div>
</header>

<section class="container py-5 text-center">
  <h2 class="mb-4">Examples of created logos</h2>
  <div class="row g-4 justify-content-center">
    <div class="col-6 col-md-4 col-lg-3"><img src="{% static 'logoi/images/logo1.png' %}" class="img-fluid rounded logo-primer" alt="Red round logo with the letters SP"></div>
    <div class="col-6 col-md-4 col-lg-3"><img src="{% static 'logoi/images/logo2.png' %}" class="img-fluid rounded logo-primer" alt="Blue square logo with the letters IT"></div>
    <div class="col-6 col-md-4 col-lg-3"><img src="{% static 'logoi/images/logo3.png' %}" class="img-fluid rounded logo-primer" alt="Green round logo with the letters FK"></div>
  </div>
</section>
{% endblock %}

The content is the same as in the frontend project; only the paths changed ({% static %}, {% url %}) and the menu moved to base.html.

Static files

logoi/
├── static/
│   └── logoi/
│       ├── css/main.css          ← from the frontend project (lesson 15)
│       ├── js/generator.js       ← from the frontend project (lesson 16)
│       └── images/  pozadina.jpg  logo1.png  logo2.png  logo3.png
└── templates/
    └── logoi/
        ├── naslovna.html
        └── generator.html

Copy the files from the frontend project to these places. The path url("../images/pozadina.jpg") in main.css still works, because css and images are in the same folder as before. For the generator, take generator.html from the frontend project for now, put it into the sadrzaj block and link the script with {% static 'logoi/js/generator.js' %}; in lesson 8 the generator form becomes a Django form.

Start the server and open http://127.0.0.1:8000/: the home page works, and a typed name leads to /generator/?naziv=....

Video: the home page in Django

Note on the video: the video was recorded for the first version of the project (an older Django version, Heroku). The ideas are the same, but the code in this lesson differs in details – where they differ, the code on this page applies.

Practice tasks

Every task has an example, hints and a solution.

TASK 01Difficulty: easy

An “About the project” page

Add the page /o-projektu/ with a heading and two paragraphs, which inherits base.html, and a link to it in the menu.

Common mistakes and tips
  • TemplateDoesNotExist: check the path logoi/templates/logoi/naslovna.html and whether the app is in INSTALLED_APPS.
  • NoReverseMatch: the name in {% url '...' %} does not exist in urls.py.
  • The CSS does not load: check {% load static %} at the top of the template and the path in static/logoi/....
  • A template that inherits another must start with {% extends %}; all content must be inside blocks.

Check your knowledge

Open the quiz

URLs, views and templates

1. What does {% extends "base.html" %} do?




2. How do you make a link in a template to the route named generator?




3. Where does Django look for the static files of the logoi app?