1. What is Django?
Answer: Django is a high-level Python web framework that encourages rapid
development and clean, pragmatic design. It follows the MVT (Model-View-Template)
architecture and helps developers build secure, scalable, and maintainable web
applications efficiently.
2. What are the key features of Django?
Answer:
MVT Architecture: Separates business logic from presentation.
Built-in Authentication: User authentication and authorization system.
ORM (Object-Relational Mapping): Interacts with the database using Python
classes.
Admin Panel: Auto-generated backend interface for managing models.
Middleware Support: Processes requests before they reach the view.
Security: Protects against SQL Injection, XSS, and CSRF attacks.
Scalability: Can handle high traffic with proper optimizations.
3. Explain the Django project structure.
Answer: When you create a Django project, you get the following structure:
myproject/
manage.py # Command-line utility for Django
myproject/ # Main project directory
__init__.py # Marks directory as a Python package
settings.py # Configuration settings
urls.py # URL routing
wsgi.py # Entry point for WSGI servers
app_name/ # Django app (created separately)
models.py # Database models
views.py # View functions or class-based views
urls.py # App-specific URLs
templates/ # HTML templates
static/ # Static files (CSS, JS, images)
4. What is MVT (Model-View-Template) in Django?
Answer: MVT is Django's software architecture pattern:
Model: Defines the database structure (tables and relationships).
View: Handles business logic and returns HTTP responses.
Template: Handles presentation and user interface.
5. How does Django handle requests?
Answer: Django processes requests in the following way:
1. User makes a request (e.g., https://example.com/home)
2. Django's URL dispatcher (urls.py) matches the request to a view function.
3. The view processes the request and interacts with the model (database) if needed.
4. The view returns a response, often rendered using a template.
5. The response is sent to the user's browser.
6. What is the role of manage.py in Django?
Answer: manage.py is a command-line utility that allows you to interact with your
Django project. Common commands include:
python manage.py runserver → Starts the development server.
python manage.py migrate → Applies database migrations.
python manage.py createsuperuser → Creates an admin user.
python manage.py shell → Opens an interactive shell.
7. What is the purpose of settings.py?
Answer: settings.py contains the configuration for the Django project, including:
Database settings (DATABASES)
Installed apps (INSTALLED_APPS)
Middleware (MIDDLEWARE)
Static and media file settings (STATIC_URL, MEDIA_URL)
Security settings (SECRET_KEY, DEBUG, ALLOWED_HOSTS)
8. What is urls.py used for in Django?
Answer: urls.py defines URL patterns that map URLs to specific views. Example:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('about/', views.about, name='about'),
]
This maps the / (root) URL to home view and /about/ to about view.
9. What are Django models?
Answer: Django models define database tables using Python classes. Example:
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=10, decimal_places=2)
Django automatically creates SQL tables based on models.
10. How to create and apply migrations in Django?
Answer: Migrations track changes in the database schema.
Create migrations: python manage.py makemigrations
Apply migrations: python manage.py migrate
11. What is Django ORM?
Answer: Django ORM (Object-Relational Mapping) allows interaction with the database
using Python instead of SQL. Example:
# Query all products
products = Product.objects.all()
# Filter products by price
cheap_products = Product.objects.filter(price__lt=500)
12. What is the difference between GET and POST methods in Django?
Answer:
GET: Used to retrieve data, parameters appear in the URL (request.GET).
POST: Used to send data securely (e.g., form submissions) (request.POST).
13. What is Django Admin, and how to use it?
Answer: Django Admin is an auto-generated backend interface.
Enable admin: Add django.contrib.admin to INSTALLED_APPS.
Create a superuser: python manage.py createsuperuser
Register models in admin.py:
from django.contrib import admin
from .models import Product
admin.site.register(Product)
14. How do you render an HTML template in Django?
Answer: Use render() function in a view:
from django.shortcuts import render
def home(request):
return render(request, 'home.html', {'title': 'Home Page'})
This renders home.html with a title variable.
15. How do you serve static files in Django?
Answer:
1. Define static settings in settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / "static"]
2. Use {% load static %} in templates:
{% load static %}
<img src="{% static 'images/logo.png' %}" alt="Logo">
16. What is csrf_token in Django?
Answer: CSRF (Cross-Site Request Forgery) token is used to protect forms from attacks.
Example:
<form method="post">
{% csrf_token %}
<input type="text" name="name">
<button type="submit">Submit</button>
</form>
Django rejects POST requests without a CSRF token.
17. What is Django middleware?
Answer: Middleware processes requests before they reach views.
Example: AuthenticationMiddleware, SecurityMiddleware.
Custom middleware example:
class CustomMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
return response
18. What is the difference between ForeignKey, OneToOneField, and ManyToManyField?
Answer:
ForeignKey → Many-to-One relationship (e.g., multiple students belong to one
class).
OneToOneField → One-to-One relationship (e.g., one user has one profile).
ManyToManyField → Many-to-Many relationship (e.g., multiple students enrolled
in multiple courses).