Python Notes for Web/Backend Developer Interviews
1. Django Basics
- Django is a high-level Python web framework for rapid development.
- Follows the MTV pattern (Model-Template-View).
- Commands to start project:
django-admin startproject projectname
python manage.py startapp appname
Important Files:
- settings.py: project settings
- urls.py: URL routing
- views.py: request handling
- models.py: database structure
2. Models and ORM
- Django ORM allows interaction with the database using Python objects.
Example Model:
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=5, decimal_places=2)
Migrations:
python manage.py makemigrations
python manage.py migrate
CRUD Operations:
Product.objects.create(name="Item", price=10.99)
Product.objects.all()
Product.objects.get(id=1)
Product.objects.filter(name="Item")
Product.objects.update(price=12.99)
Product.objects.get(id=1).delete()
3. Views and URLs
Views:
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello World")
URL Routing:
Python Notes for Web/Backend Developer Interviews
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
Types of Views:
- Function-based views (FBV)
- Class-based views (CBV)
4. Templates
- Django uses a templating engine to render dynamic HTML.
Using Template:
return render(request, 'home.html', {'name': 'Abhinav'})
Template Tags:
{{ variable }}
{% if user.is_authenticated %}
{% for item in items %}
{% csrf_token %}
5. Forms
Django Forms:
from django import forms
class ContactForm(forms.Form):
name = forms.CharField()
email = forms.EmailField()
Handling Form in Views:
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
# process form data
Using ModelForm:
class ProductForm(forms.ModelForm):
class Meta:
model = Product
fields = '__all__'
6. Django Admin
Python Notes for Web/Backend Developer Interviews
- Powerful admin interface for managing models
Enable Admin:
from django.contrib import admin
from .models import Product
admin.site.register(Product)
Create Superuser:
python manage.py createsuperuser
7. User Authentication
Built-in User Auth:
- Login, logout, password change, reset
from django.contrib.auth import authenticate, login, logout
Login:
user = authenticate(username='abhi', password='123')
if user is not None:
login(request, user)
Logout:
logout(request)
8. Django REST Framework (DRF)
Installation:
pip install djangorestframework
Serializers:
from rest_framework import serializers
from .models import Product
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = '__all__'
Views:
from rest_framework import viewsets
class ProductViewSet(viewsets.ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer
Python Notes for Web/Backend Developer Interviews
Routers:
from rest_framework.routers import DefaultRouter
router = DefaultRouter()
router.register(r'products', ProductViewSet)
urlpatterns += router.urls
9. Deployment Tips
- Use gunicorn + nginx for production
- Use PostgreSQL in production
- Set DEBUG = False
- Configure ALLOWED_HOSTS
- Use environment variables for secrets