Get ready for your next interview with our comprehensive question library
Django is a high-level Python web framework that follows the Model-View-Template (MVT) architectural pattern. It emphasizes rapid development and clean, pragmatic design.
Key features include:
MVT is Django's architectural pattern:
The flow: URL dispatcher routes requests to appropriate View → View processes request and interacts with Model → View renders Template with data → Response sent to user.
Django ORM (Object-Relational Mapping) is a layer that allows you to interact with databases using Python code instead of SQL. It maps database tables to Python classes and rows to objects.
Advantages:
Example:
# urls.py
urlpatterns = [
path('articles/', views.article_list, name='article_list'),
]
# views.py
def article_list(request):
articles = Article.objects.all()
return render(request, 'articles/list.html', {'articles': articles})
A project can contain multiple apps, and apps can be reused across different projects. Apps should follow the single responsibility principle - each app should have one clear purpose.
Example structure:
myproject/ # Project
├── blog/ # App
├── users/ # App
├── settings.py # Project settings
└── urls.py # Project URLs
Django model fields define the data types and constraints for database columns.
Common field types:
class Article(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
published_date = models.DateTimeField(auto_now_add=True)
is_published = models.BooleanField(default=False)
author = models.ForeignKey(User, on_delete=models.CASCADE)
tags = models.ManyToManyField(Tag)
email = models.EmailField()
slug = models.SlugField(unique=True)
Function-based views (FBV): Simple functions that take request and return response
def article_list(request):
articles = Article.objects.all()
return render(request, 'articles/list.html', {'articles': articles})
Class-based views (CBV): Classes that inherit from Django's view classes
from django.views.generic import ListView
class ArticleListView(ListView):
model = Article
template_name = 'articles/list.html'
context_object_name = 'articles'
CBVs provide more structure and reusability, while FBVs are simpler and more explicit.
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime