Django Complete Preparation Guide
1. What is Django?
Django is a high-level Python web framework that allows you to build secure, maintainable websites quickly.
Why Django?
- Fast: Rapid development
- Secure: Handles common security mistakes
- Scalable: Used by Instagram, Pinterest
- Batteries-included: Admin, auth, ORM, etc.
2. Installation and Setup
1. Install Python and pip.
2. Create a virtual environment:
python -m venv env
source env/bin/activate
3. Install Django: pip install django
4. Create a project:
django-admin startproject myproject
cd myproject
python manage.py runserver
Then open http://127.0.0.1:8000 in your browser.
3. Django Project vs App
Project: Entire website (container)
App: Reusable component or feature
Create an app:
python manage.py startapp blog
4. Views, URLs, Templates (MVT Pattern)
Model - Database
View - Logic
Template - HTML page
Create view in blog/views.py and map in blog/urls.py
Include blog.urls in main project's urls.py
Django Complete Preparation Guide
5. Models and ORM
Define models in models.py
Use makemigrations and migrate to update the database
Example:
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
6. Admin Panel
Register models in admin.py
Create a superuser with:
python manage.py createsuperuser
Access admin at /admin
7. Templates and Static Files
Templates are HTML files stored in templates/appname/
Use render(request, template, context) in views
Static files are for CSS, JS, images
8. Forms and CRUD
Use HTML forms with POST method
Handle POST request in views and save data using model.objects.create()
CRUD: Create, Read, Update, Delete operations
9. User Authentication
Django provides built-in user login, logout, registration via django.contrib.auth
Use auth_views in urls.py
10. Interview Questions
- What is Django?
- What is ORM?
Django Complete Preparation Guide
- What is middleware?
- Django vs Flask?
- What is CSRF?
- How to create REST API?
- What is QuerySet?
Bonus: Mini Project Idea
Blog App:
- View posts
- Login to create/edit/delete
- Admin panel to manage all
Django Command Cheat Sheet
- django-admin startproject name
- python manage.py startapp name
- runserver, makemigrations, migrate, shell
- createsuperuser