🌐 Core Django Concepts – Mastering Views, Templates, Models & URLs
🧲 Introduction – Why Core Django Concepts Matter
Once your Django project is set up, mastering its core components is essential to build powerful web applications. These components include views, templates, models, and URL routing—all working together to deliver content dynamically and efficiently.
🎯 In this guide, you’ll explore:
- How Django views connect data to frontend
- Setting up URLs for page routing
- Creating and rendering templates
- Defining and using models for database operations
- Structuring layout with base templates and dynamic pages
📘 Topics Covered
| 🔹 Topic | 📄 Description |
|---|---|
| 🔍 Django Views | Handle requests and return responses using Python functions or classes |
| 🔗 Django URLs | Map URLs to views using routing patterns |
| 🖼️ Django Templates | Render HTML dynamically with Django’s template engine |
| 🗃️ Django Models | Define and interact with database tables |
| 📊 Django Display Data | Use views and templates to display data from models |
| 🧰 Django Prep Template and View | Set up view functions and templates to prepare dynamic rendering |
| 🔗 Django Add Link to Details | Add clickable links for navigating to detail views |
| 🧱 Django Add Master Template | Create a reusable base layout for consistency across pages |
| 📄 Django Add Main Index Page | Build the homepage to show list or landing content |
| 🚫 Django 404 Template | Customize error pages for a better user experience |
| 🧪 Django Add Test View | Test setup and rendering of views during development |
🔍 Django Views
Views are Python functions or class-based methods that receive HTTP requests and return HTTP responses.
from django.http import HttpResponse
def index(request):
return HttpResponse("Welcome to the homepage!")
➡️ These views are connected to URLs and can render templates or return JSON responses.
🔗 Django URLs
URLs define the paths that users navigate to in your application. Use urls.py to map these paths to views.
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
🧭 Each route is clean, human-readable, and scalable.
🖼️ Django Templates
Templates allow you to embed dynamic content inside HTML using Django Template Language (DTL).
<!-- index.html -->
<h1>Welcome {{ user.username }}</h1>
Templates are rendered in views using render():
from django.shortcuts import render
def index(request):
return render(request, 'index.html', {'user': request.user})
🗃️ Django Models
Models define database schema using Python classes.
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=100)
body = models.TextField()
Use makemigrations and migrate to update the database schema.
📊 Django Display Data
To display data from the database:
def article_list(request):
articles = Article.objects.all()
return render(request, 'articles/list.html', {'articles': articles})
Templates will loop through the data:
{% for article in articles %}
<h2>{{ article.title }}</h2>
<p>{{ article.body }}</p>
{% endfor %}
🧰 Django Prep Template and View
Organize your views to handle logic and connect it to corresponding templates. Always keep logic in the view and UI in templates.
🔗 Django Add Link to Details
In your list template:
<a href="{% url 'article_detail' article.id %}">Read More</a>
Update your urls.py:
path('article/<int:id>/', views.article_detail, name='article_detail')
🧱 Django Add Master Template
Create base.html and extend it in all other templates:
<!-- base.html -->
<!DOCTYPE html>
<html>
<head><title>{% block title %}My Site{% endblock %}</title></head>
<body>
<header>Navbar</header>
{% block content %}{% endblock %}
</body>
</html>
📄 Django Add Main Index Page
Make your homepage informative and dynamic using index.html. Include a list of items, search bar, or introductory content.
🚫 Django 404 Template
Create 404.html and place it in your templates directory to show a custom error page when a user hits a non-existent route.
🧪 Django Add Test View
Test views with different data before production:
def test(request):
return render(request, 'test.html', {'data': 'Test Mode'})
Use for debugging and temporary checks.
📌 Summary – Recap & Next Steps
- 🧩 Views connect Python logic to user interfaces
- 🧭 URLs define routing paths
- 🖼️ Templates render dynamic HTML
- 🗃️ Models handle database structure and operations
- 🧱 Base templates improve code reuse and page consistency
⚙️ These core concepts build the heart of any Django-powered web app.
❓ FAQs – Core Django Concepts
❓ What’s the difference between a view and a template in Django?
✅ A view handles logic and data, while a template is responsible for rendering HTML content shown to users.
❓ How do Django URLs work?
✅ Django maps each URL pattern to a specific view. You define them in urls.py.
❓ Why should I use a base template?
✅ It helps keep a consistent layout (like headers/footers) and avoids duplicating code.
❓ How do I customize 404 error pages in Django?
✅ Create a 404.html template and ensure DEBUG = False in settings for it to display.
❓ What is the purpose of test views?
✅ Test views help in debugging and ensuring templates render correctly during development.
Share Now :
