Performance: How Django processes a request (URL → View → Template)
This concept affects the server response time and how quickly the browser receives the rendered HTML.
urls.py: from django.urls import path from . import views urlpatterns = [ path('profile/<int:user_id>/', views.profile_view), ] views.py: from django.shortcuts import render from .models import User, Post def profile_view(request, user_id): user = User.objects.get(id=user_id) posts = Post.objects.filter(user_id=user_id) # Fetch only relevant posts return render(request, 'profile.html', {'user': user, 'posts': posts})
urls.py: from django.urls import path from . import views urlpatterns = [ path('profile/<int:user_id>/', views.profile_view), ] views.py: from django.shortcuts import render from .models import User, Post def profile_view(request, user_id): user = User.objects.get(id=user_id) posts = Post.objects.all() # Fetching all posts unnecessarily return render(request, 'profile.html', {'user': user, 'posts': posts})
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Fetching all posts in view | N/A (server-side) | N/A | N/A | [X] Bad |
| Filtering posts by user in view | N/A (server-side) | N/A | N/A | [OK] Good |
urls.py?path() to define URL patterns, with URL string, view function, and optional name.url() is deprecated, and route() or link() are invalid.{{ message }}?
def hello_view(request):
context = {'message': 'Hello, Django!'}
return render(request, 'hello.html', context){{ message }} to display the value 'Hello, Django!'.def my_view(request):
return render('my_template.html')/products/. The URL pattern matches products_view. The view fetches products from the database and passes them to products.html template. The template shows the product names in a list.
Which is the correct order of steps Django follows internally?Pass the test and this pattern moves unknown