Views decide what data to show; templates decide how it looks. Django’s template language is simple on purpose, and one feature in particular, template inheritance, saves you from copying the same header and footer into every page.
Variables and filters
Pass data from a view with render(request, "courses/list.html", {"courses": courses}). In the template, show values with double braces: {{ course.title }}. Filters change how a value is displayed: {{ course.title|upper }}, {{ created_at|date:"d M Y" }}, or {{ description|truncatewords:20 }}.
Tags for logic
- Loops:
{% for course in courses %} ... {% empty %} No courses yet {% endfor %} - Conditions:
{% if user.is_authenticated %} ... {% endif %} - Links:
{% url "course-detail" course.id %}builds URLs from route names, so links do not break when paths change.
Keep heavy logic in views or models. Templates should only display data.
Template inheritance
Create one base.html with the shared layout and empty blocks:
<html><body>
{% include "partials/nav.html" %}
{% block content %}{% endblock %}
</body></html>
Every other page extends it and fills only the block:
{% extends "base.html" %}
{% block content %} <h1>Courses</h1> ... {% endblock %}
Change the navigation once in base.html and every page updates. You can add more blocks, such as title or scripts, for page-specific parts.
Static files
Load CSS and images with {% load static %} and {% static "css/site.css" %}. In production, run collectstatic so the web server can serve them.
Security note
Django escapes variables automatically, which protects against script injection. Avoid the safe filter on anything a user typed.
Related: Django models and migrations, Django forms and validation and Django authentication.
These topics are practised with projects in our Python Full Stack course in Bangalore.
Book a free demo class at our BTM Layout centre, and ask a counsellor for batch details.
