DjangoWeb DevelopmentPython

Getting Started with Django: A Practical Introduction

Learn Django's core concepts by building a tiny project. Fast setup, URL routing, views, templates, and models explained simply.

WebPrims Team18 August 20263 min read
Getting Started with Django: A Practical Introduction

Django is a high-level Python web framework that lets you build secure, scalable web apps without reinventing the wheel. It comes with an admin interface, an ORM, authentication, and URL routing built in — so you can focus on what makes your app unique. This guide walks you through the essentials with a hands-on example. No prior Django experience needed, just basic Python.

Why Choose Django?

Django follows the "batteries included" philosophy. Instead of picking separate libraries for databases, forms, or user management, you get them all from the start. That means:

  • Rapid development — you can go from idea to working prototype in hours.
  • Security — Django helps prevent SQL injection, XSS, CSRF, and other common attacks.
  • Scalability — used by Instagram, Pinterest, and Disqus, so it handles serious traffic.
  • Huge community — tons of packages, docs, and tutorials are available.

But Django isn't magic. You still need to understand its main building blocks: models, views, templates, and URLs. That's what we'll cover now.

Setting Up Your Environment

First, make sure Python 3.10+ is installed. Then create a virtual environment and install Django:

python -m venv myenv
source myenv/bin/activate  # on Windows: myenv\Scripts\activate
pip install django

Once installed, create a new project and an app within it. A project holds configuration, while an app is a specific module (like a blog or a poll). For this example, we'll build a simple "hello" page that loads content from the database.

django-admin startproject myproject
cd myproject
python manage.py startapp hello

Now register the app in myproject/settings.py — add 'hello' to the INSTALLED_APPS list.

The Request/Response Cycle

When a user visits your site, the browser sends a request. Django first checks the URL patterns (in urls.py), then passes the request to the matching view. The view does some logic (often reading from a database), and returns an HTTP response — usually an HTML page.

Let's create a simple view. Open hello/views.py and write:

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, Django!")

Now we need to map a URL to this view. Create a file hello/urls.py and add:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

Then include the app's URLs in the project's urls.py:

from django.urls import include, path

urlpatterns = [
    path('', include('hello.urls')),
]

Run python manage.py runserver and visit http://localhost:8000 — you'll see "Hello, Django!". That's the core cycle.

Using Templates to Separate Logic from Presentation

Hardcoding HTML in Python is messy. That's where templates come in. Create a template directory inside the hello app: hello/templates/hello/. Then create a file index.html:

<!DOCTYPE html>
<html>
<head><title>Hello</title></head>
<body>
    <h1>{{ greeting }}</h1>
</body>
</html>

Update the view to render it:

from django.shortcuts import render

def index(request):
    context = {'greeting': 'Hello from a template!'}
    return render(request, 'hello/index.html', context)

Now your HTML is in one place, logic in another. Django templates support variables ({{ }}), tags ({% if %}), and filters ({{ value|upper }}).

Models: Your Database without SQL

Models are Python classes that define database tables. Add a simple model to hello/models.py:

from django.db import models

class Message(models.Model):
    text = models.CharField(max_length=100)
    created_at = models.DateTimeField(auto_now_add=True)

Now generate and run migrations:

python manage.py makemigrations
python manage.py migrate

Django creates the table automatically. You can also get a free admin interface — just create a superuser (python manage.py createsuperuser), register the model in hello/admin.py, and log into /admin to add records.

To use the database in a view, query it like this:

def index(request):
    messages = Message.objects.all()
    return render(request, 'hello/index.html', {'messages': messages})

This is just a taste of the ORM's power with filtering, ordering, and aggregations.

What's Next?

You now understand the Django architecture: URL → View → Model → Template. Next, you can explore class-based views, forms, Django REST Framework for APIs, and user authentication — all built on the same solid foundation.

Django might feel heavy at first, but the conventions save you time in the long run. Start with a tiny project like a to-do list or a blog. Read the official docs when you get stuck, and don't be afraid to experiment. Your future self will thank you.

Come and sit a class before you decide

Reading about it only gets you so far. Pick a day, sit in on a class that is already running, write some code, and talk to the students in it. Free, and nothing to pay afterwards unless you want to join.

DjangoWeb DevelopmentPython

Found this useful?

Share it with someone who is learning.