Python Development

Mastering Django: A Comprehensive Guide

By the Domain India teamPublished 8 min read
Knowledge base article
Contents (9 sections)

Django is a free, open-source Python web framework for building database-driven websites quickly and safely. It ships with an ORM, migrations, an admin site, authentication, forms and templates, so you write your application rather than glue code. This guide takes you from an empty folder to a working app with a model, the admin, views, templates and tests, and ends with how to deploy it.

Key takeaways

Install a supported Python, create a virtual environment with python -m venv, install Django, and start a project with django-admin startproject. Define models, run makemigrations and migrate, register models in the admin, then add views, URLs and templates. Before going live, set DEBUG = False, keep secrets in environment variables and run python manage.py check --deploy. On Domain India you can run a small Django site on cPanel or DirectAdmin shared hosting, or use the App Platform or a VPS for production apps.

1. Why Django

Good for
  • Content sites, dashboards, internal tools and CRUD-heavy apps
  • An automatic admin site generated from your models
  • Security defaults: CSRF protection, auto-escaping templates, parameterised ORM queries
  • A mature ecosystem, including Django REST Framework for APIs
Watch out for
  • Heavier than Flask for a tiny single-endpoint service
  • Async support exists but most of the ecosystem is still synchronous
  • Background jobs need a separate worker process

Django calls its pattern MTV: the model is your data, the template is the HTML, and the view is the Python function or class that fetches data and chooses a template. It maps closely to MVC; Django's "view" does the controller's job.

2. Set up your environment

Pick a Django release that is still supported. Django publishes a supported-versions table on djangoproject.com; a long-term support (LTS) release gets security fixes for about three years, and each release lists the Python versions it supports. Match the Python version on your computer to the one you will deploy on.

bash
mkdir mysite-project && cd mysite-project
python3 -m venv .venv
source .venv/bin/activate          # Windows: .venv\Scripts\activate
python -m pip install --upgrade pip
python -m pip install django
python -m django --version

python -m venv is built into Python, so you don't need the separate virtualenv package. Record your dependencies so the server installs the same versions:

bash
python -m pip freeze > requirements.txt

3. Projects and apps

A project is the whole site: settings, root URLs and the WSGI/ASGI entry points. An app is one feature module, such as a blog or a shop, and a project can hold many.

bash
django-admin startproject mysite .
python manage.py startapp inventory
python manage.py runserver

Open http://127.0.0.1:8000/ to see the welcome page. Then add "inventory" to INSTALLED_APPS in mysite/settings.py.

4. Models and migrations

Models are Python classes; Django turns them into tables.

python
# inventory/models.py
from django.db import models

class Supplier(models.Model):
    name = models.CharField(max_length=120)
    email = models.EmailField(blank=True)

    def __str__(self):
        return self.name

class Product(models.Model):
    supplier = models.ForeignKey(Supplier, on_delete=models.PROTECT, related_name="products")
    name = models.CharField(max_length=200)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    in_stock = models.BooleanField(default=True)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ["name"]

    def __str__(self):
        return self.name
bash
python manage.py makemigrations
python manage.py migrate

Commit migration files to version control and run migrate on every deploy. Use ForeignKey for one-to-many, OneToOneField for one-to-one and ManyToManyField for many-to-many relationships. Store money in DecimalField, never FloatField.

5. The admin site

python
# inventory/admin.py
from django.contrib import admin
from .models import Product, Supplier

@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display = ["name", "supplier", "price", "in_stock"]
    list_filter = ["in_stock", "supplier"]
    search_fields = ["name"]

admin.site.register(Supplier)

Create a login with python manage.py createsuperuser and open /admin/. The admin is meant for trusted staff, not for customers.

6. Views, URLs and templates

python
# inventory/views.py
from django.shortcuts import get_object_or_404, render
from .models import Product

def product_list(request):
    products = Product.objects.filter(in_stock=True).select_related("supplier")
    return render(request, "inventory/product_list.html", {"products": products})

def product_detail(request, pk):
    product = get_object_or_404(Product, pk=pk)
    return render(request, "inventory/product_detail.html", {"product": product})
python
# inventory/urls.py
from django.urls import path
from . import views

app_name = "inventory"
urlpatterns = [
    path("", views.product_list, name="list"),
    path("<int:pk>/", views.product_detail, name="detail"),
]

Include it from mysite/urls.py with path("products/", include("inventory.urls")). Templates go in inventory/templates/inventory/; Django escapes variables automatically, so {{ product.name }} is safe to print. select_related fetches the supplier in the same query and avoids the classic "N+1 queries" slowdown.

7. Forms, users and tests

  • Forms. A ModelForm builds and validates a form from a model. Always include {% csrf_token %} in POST forms.
  • Users. django.contrib.auth gives you login, logout, password reset and permissions. Start a new project with a custom user model if you might ever need extra user fields; changing it later is hard.
  • Tests. Put tests in tests.py and run python manage.py test. Django creates a throwaway test database for each run.
python
from django.test import TestCase
from django.urls import reverse
from .models import Product, Supplier

class ProductListTests(TestCase):
    def test_lists_in_stock_products(self):
        s = Supplier.objects.create(name="Acme")
        Product.objects.create(supplier=s, name="Widget", price="99.00")
        response = self.client.get(reverse("inventory:list"))
        self.assertContains(response, "Widget")

8. Before you deploy

  1. Turn debug off.
    Set DEBUG = False and list your domains in ALLOWED_HOSTS and CSRF_TRUSTED_ORIGINS (with https://).
  2. Move secrets out of code.
    Read SECRET_KEY and database passwords from environment variables.
  3. Collect static files.
    Set STATIC_ROOT and run python manage.py collectstatic.
  4. Use a production database
    such as MySQL or PostgreSQL rather than SQLite for anything with real traffic.
  5. Run the checklist.
    python manage.py check --deploy warns about insecure settings such as missing HTTPS cookie flags.
  6. Serve with a WSGI server,
    never runserver, which is for development only.
Never deploy with DEBUG = True

With debug on, an error page shows your settings, file paths and parts of your code to anyone who triggers it. Keep it off in production and read errors from your logs instead.

9. Running Django on Domain India

OptionHow Django runsBest for
cPanel or DirectAdmin shared hostingThe panel's Python app tool, loaded through passenger_wsgi.pySmall sites and low-traffic apps
App PlatformYour own Dockerfile; deploy with Deploy Now or a deploy tokenProduction apps that need clean deploys, with PostgreSQL included
VPSYour choice of Gunicorn behind Nginx, with root accessCelery workers, Redis, custom system packages

On shared hosting, the Python app tool creates the virtual environment and installs requirements.txt for you; when we checked the servers on 23 September 2026, cPanel offered Python 3.9, 3.11 and 3.12. Check that your Django release supports the Python version you pick. Background workers such as Celery do not stay running on shared hosting. The full setup is in How to deploy a Python app on shared hosting. Jailed SSH access is available on every shared hosting plan; it is off by default, so ask support to enable it.

For the App Platform, read Getting started with the App Platform. Prices are Domain India list prices on 19 September 2026, excluding 18% GST.

App Starter
₹100/mo + GST
  • 512 MB RAM per app
  • 1 vCPU
  • 5 GB NVMe SSD
  • PostgreSQL Database
See plan details
Which Django version should I use?

A release that is still supported, as listed on djangoproject.com. Choose the long-term support release if you want the longest security-fix window, and check that it supports the Python version on your server.

Do I need virtualenv to use Django?

No. Python's built-in venv module creates virtual environments; run python -m venv .venv and activate it before installing Django.

Can I run Django on Domain India shared hosting?

Yes. On our cPanel and DirectAdmin servers, the control panel's Python app tool runs Django as a WSGI app through a passenger_wsgi.py file. For background workers or heavy traffic, use the App Platform or a VPS.

Is the Django admin safe to use in production?

Yes, for trusted staff. Use strong passwords, serve it only over HTTPS, and don't give customers admin accounts.

What does python manage.py check --deploy do?

It checks your settings for common production problems, such as DEBUG left on, missing ALLOWED_HOSTS and insecure cookie settings, and prints a warning for each.

Can I run Celery with Django on shared hosting?

No. Long-running worker processes are stopped on shared hosting. Run Celery on a VPS.

Ready to deploy your Django app? Try it on cPanel hosting, move it to the App Platform, or choose a VPS for full control. If you are unsure which fits, open a support ticket.

Run your Django app on the App Platform

Deploy from your Dockerfile with Deploy Now or a deploy token, with PostgreSQL and SSL included.

See App Platform plans

Was this article helpful?

Your answer helps us decide what to improve next.

Still need help? Open a support ticket and our team will reply.

Prefer an app? Add this site to your home screen.Get the app
Mastering Django: Models, Admin, Views and Deployment