Quiz on AI Interviews Prep Live Training Corporate Training

DJango Tutorials.

A beginner-to-intermediate guide to the Django web framework.

1. Introduction to Django

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.

  • Built-in admin interface
  • Secure by default
  • Scalable
  • Follows MVT (Model-View-Template)

2. Installing Django

# Create virtual environment
python -m venv venv

# Activate environment
source venv/bin/activate   # Linux/macOS
venv\Scripts\activate      # Windows

# Install Django
pip install django

# Verify installation
django-admin --version

3. Creating a Django Project

django-admin startproject myproject
cd myproject
python manage.py runserver

Visit http://127.0.0.1:8000 to see the Django welcome page.

4. Django Project Structure

myproject/
│── manage.py
│── myproject/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── asgi.py
│   └── wsgi.py

5. Creating an App

python manage.py startapp blog

Add the app to INSTALLED_APPS in settings.py.

6. Models

from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

7. Migrations

python manage.py makemigrations
python manage.py migrate

8. Django Admin

python manage.py createsuperuser
from django.contrib import admin
from .models import Post

admin.site.register(Post)

Access admin panel at /admin.

9. Views

from django.http import HttpResponse

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

10. URLs

from django.urls import path
from .views import home

urlpatterns = [
    path('', home, name='home'),
]

11. Templates

<!-- templates/home.html -->
<h1>Welcome to Django</h1>
<p>This is a template</p>

12. Forms

from django import forms

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ['title', 'content']

13. Authentication

from django.contrib.auth.decorators import login_required

login_required
def dashboard(request):
    return render(request, "dashboard.html")

14. Django ORM

# Create
Post.objects.create(title="Post 1", content="Hello")

# Read
Post.objects.all()

# Update
post = Post.objects.get(id=1)
post.title = "Updated"
post.save()

# Delete
post.delete()

15. Deployment Basics

  • Set DEBUG = False
  • Configure allowed hosts
  • Use Gunicorn / uWSGI
  • Use Nginx as reverse proxy