Skip to content

Django — Intermediate (50+ Coding Exercises)

  1. Convert Book CRUD views to class-based generic views (List/Detail/Create/Update/Delete).
  2. Add LoginRequiredMixin and test redirects for anonymous users.
  3. Add PermissionRequiredMixin for delete permission and configure permissions.
  4. Implement Q-based search across multiple fields (title OR author name).
  5. Implement advanced filters: min_price, max_price, published_after.
  6. Add database indexes to fields used in filters and create migrations.
  7. Implement UniqueConstraint for (author, slug) and handle conflicts.
  8. Add server-side form validation in clean() for Book (e.g., price >= 0).
  9. Implement transaction.atomic() around a multi-model create workflow.
  10. Build a bulk create endpoint that accepts JSON list and creates multiple books (no DRF).
  11. Add Django messages (success/error) to CRUD workflows.
  12. Build a reusable template fragment for form rendering (include/partial).
  13. Create a custom context processor that injects site-wide settings into templates.
  14. Create a custom template tag active_nav that highlights current menu item.
  15. Implement session-based recently viewed books list (store last 5 ids).
  16. Add caching for the book list page (5 minutes) and cache-bust on book save.
  17. Add template fragment caching for a “top rated books” widget.
  18. Add a middleware that blocks requests without a required header in production mode.
  19. Add a middleware that rate-limits requests per IP (simple cache-based counter).
  20. Optimize N+1 queries on list/detail pages using select_related/prefetch_related.
  21. Write a view that outputs query count and time (dev only) for profiling.
  22. Add file upload with ImageField and create a thumbnail on save (Pillow).
  23. Add a cleanup task to delete old uploads not referenced by any model.
  24. Implement a CSV import page: upload CSV, preview parsed rows, then commit.
  25. Implement CSV export for filtered results (stream response).
  26. Add a management command to backfill slugs for existing books.
  27. Add a management command to recalculate and store avg_rating field.
  28. Add signals to update aggregate fields when reviews are created/updated/deleted.
  29. Implement soft delete for Book (flag + query manager excluding deleted by default).
  30. Add an admin action to “restore” soft-deleted books.
  31. Add a custom admin filter for soft-deleted vs active.
  32. Add unit tests for QuerySet filters and ordering.
  33. Add integration tests using Django test Client for login-required views.
  34. Add pytest fixtures/factory-like helpers (even if using unittest style) to create data quickly.
  35. Add i18n: mark strings for translation in templates and compile messages (workflow).
  36. Create a localized date display filter and apply it to templates.
  37. Add a “profile” app with user settings; create a form to update settings.
  38. Add email sending on review creation using console backend; test it.
  39. Add a password reset flow using Django auth views and templates.
  40. Implement a simple API token model and middleware-based token auth for JSON endpoints.
  41. Add throttling for token-auth endpoints separately from normal pages.
  42. Add a sitemap XML endpoint for books and authors.
  43. Add a robots.txt view and ensure correct content-type.
  44. Implement “draft/published” state for books and hide drafts from public pages.
  45. Add staff-only view to list drafts and publish/unpublish.
  46. Add custom error pages (403/404/500) and test them.
  47. Split settings into base.py/dev.py/prod.py and load via DJANGO_SETTINGS_MODULE.
  48. Add whitenoise static serving settings and run collectstatic (config-only task).
  49. Add DB-level constraint that published_date cannot be in the future (or validate in model).
  50. Add database transaction test ensuring rollback happens on error.
  51. Add admin autocomplete for related author selection.
  52. Add a History model and record changes on every book update (simple audit trail).