Django — Intermediate (50+ Coding Exercises)
- Convert
BookCRUD views to class-based generic views (List/Detail/Create/Update/Delete). - Add
LoginRequiredMixinand test redirects for anonymous users. - Add
PermissionRequiredMixinfor delete permission and configure permissions. - Implement
Q-based search across multiple fields (title OR author name). - Implement advanced filters:
min_price,max_price,published_after. - Add database indexes to fields used in filters and create migrations.
- Implement
UniqueConstraintfor(author, slug)and handle conflicts. - Add server-side form validation in
clean()forBook(e.g., price >= 0). - Implement
transaction.atomic()around a multi-model create workflow. - Build a bulk create endpoint that accepts JSON list and creates multiple books (no DRF).
- Add Django messages (success/error) to CRUD workflows.
- Build a reusable template fragment for form rendering (include/partial).
- Create a custom context processor that injects site-wide settings into templates.
- Create a custom template tag
active_navthat highlights current menu item. - Implement session-based recently viewed books list (store last 5 ids).
- Add caching for the book list page (5 minutes) and cache-bust on book save.
- Add template fragment caching for a “top rated books” widget.
- Add a middleware that blocks requests without a required header in production mode.
- Add a middleware that rate-limits requests per IP (simple cache-based counter).
- Optimize N+1 queries on list/detail pages using
select_related/prefetch_related. - Write a view that outputs query count and time (dev only) for profiling.
- Add file upload with
ImageFieldand create a thumbnail on save (Pillow). - Add a cleanup task to delete old uploads not referenced by any model.
- Implement a CSV import page: upload CSV, preview parsed rows, then commit.
- Implement CSV export for filtered results (stream response).
- Add a management command to backfill slugs for existing books.
- Add a management command to recalculate and store
avg_ratingfield. - Add signals to update aggregate fields when reviews are created/updated/deleted.
- Implement soft delete for
Book(flag + query manager excluding deleted by default). - Add an admin action to “restore” soft-deleted books.
- Add a custom admin filter for soft-deleted vs active.
- Add unit tests for QuerySet filters and ordering.
- Add integration tests using Django test
Clientfor login-required views. - Add pytest fixtures/factory-like helpers (even if using unittest style) to create data quickly.
- Add i18n: mark strings for translation in templates and compile messages (workflow).
- Create a localized date display filter and apply it to templates.
- Add a “profile” app with user settings; create a form to update settings.
- Add email sending on review creation using console backend; test it.
- Add a password reset flow using Django auth views and templates.
- Implement a simple API token model and middleware-based token auth for JSON endpoints.
- Add throttling for token-auth endpoints separately from normal pages.
- Add a sitemap XML endpoint for books and authors.
- Add a robots.txt view and ensure correct content-type.
- Implement “draft/published” state for books and hide drafts from public pages.
- Add staff-only view to list drafts and publish/unpublish.
- Add custom error pages (403/404/500) and test them.
- Split settings into
base.py/dev.py/prod.pyand load viaDJANGO_SETTINGS_MODULE. - Add
whitenoisestatic serving settings and runcollectstatic(config-only task). - Add DB-level constraint that
published_datecannot be in the future (or validate in model). - Add database transaction test ensuring rollback happens on error.
- Add admin autocomplete for related author selection.
- Add a
Historymodel and record changes on every book update (simple audit trail).