0% found this document useful (0 votes)
33 views

Django_Notes_Backend_Developer

This document provides essential notes for Python web/backend developer interviews, focusing on Django framework basics, including project setup, models and ORM, views and URLs, templates, forms, admin interface, user authentication, and Django REST Framework. It covers commands, important files, CRUD operations, and deployment tips. The notes serve as a comprehensive guide for understanding Django's structure and functionalities.

Uploaded by

abhinavdogra649
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Django_Notes_Backend_Developer

This document provides essential notes for Python web/backend developer interviews, focusing on Django framework basics, including project setup, models and ORM, views and URLs, templates, forms, admin interface, user authentication, and Django REST Framework. It covers commands, important files, CRUD operations, and deployment tips. The notes serve as a comprehensive guide for understanding Django's structure and functionalities.

Uploaded by

abhinavdogra649
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy