Skip to content

Django — Basic (50+ Coding Exercises)

  1. Create a new Django project and app; verify it runs with runserver.
  2. Create a Book model with title, published_date, price; run migrations.
  3. Register Book in Django admin and verify CRUD in admin UI.
  4. Create a Book list page using a function-based view and template.
  5. Add a Book detail page at /books/<id>/.
  6. Implement a “Create Book” form using ModelForm.
  7. Implement “Update Book” and “Delete Book” views with confirmation page.
  8. Add pagination to the Book list page (10 per page).
  9. Add search by title with ?q= query parameter (case-insensitive).
  10. Add ordering by published_date with ?sort=published_date.
  11. Create an Author model and add ForeignKey from Book to Author.
  12. Show author name on book detail page; list author’s books on author detail page.
  13. Add a ManyToManyField tags to Book and create a Tag model.
  14. Build a page to filter books by tag.
  15. Add a slug field to Book; auto-generate it from title on save.
  16. Create a URL pattern using slug instead of id for book detail.
  17. Add a custom admin list display with search and filters for Book.
  18. Add admin inline editing for related models (e.g., Chapter inline under Book).
  19. Create a Chapter model related to Book and show chapters on book detail.
  20. Create a management command to import books from a JSON file.
  21. Create a management command to export all books to CSV.
  22. Create a middleware that adds a request ID to request and response headers.
  23. Log each request method/path/status code with timing in middleware.
  24. Create a custom template filter to format currency for price.
  25. Create a custom template tag that renders “New” badge for recently published books.
  26. Implement user login/logout using Django auth templates.
  27. Protect Create/Update/Delete views with login_required.
  28. Add “staff only” restriction to Delete view.
  29. Create a user profile model with OneToOneField to User.
  30. Auto-create profile when a user is created (signals).
  31. Add a profile edit page for logged-in users.
  32. Add file upload field cover_image to Book and configure media serving in dev.
  33. Validate uploaded cover images by size and extension in the form.
  34. Add a “featured” boolean field and show featured books separately on homepage.
  35. Create a JSON endpoint /api/books/ returning list of books (no DRF).
  36. Add a JSON endpoint /api/books/<id>/ returning a single book.
  37. Implement basic error handling in JSON endpoints (404, validation errors).
  38. Add CSRF-protected form submission and confirm CSRF errors are handled.
  39. Add a contact form using forms.Form and send an email using console email backend.
  40. Implement a simple “like” feature: BookLike model linking user+book, unique constraint.
  41. Add a button to like/unlike a book; show like count.
  42. Prevent N+1 queries on the book list using select_related/prefetch_related.
  43. Add a sitemap-like page listing all URLs for books and authors.
  44. Add a base template with navbar and extend it in all pages.
  45. Implement custom 404 page template.
  46. Create unit tests for Book model (slug generation) and one view (list view 200).
  47. Create tests for permission: anonymous user cannot access Create view.
  48. Create a fixture or factory to generate test books.
  49. Add environment-based settings: DEBUG, ALLOWED_HOSTS, SECRET_KEY from env vars.
  50. Configure static files and add one custom CSS file; verify it loads.
  51. Add a Review model linked to Book and build a form to add a review on the detail page.
  52. Add average rating calculation and show it on book list.