Django — Basic (50+ Coding Exercises)
- Create a new Django project and app; verify it runs with
runserver. - Create a
Bookmodel withtitle,published_date,price; run migrations. - Register
Bookin Django admin and verify CRUD in admin UI. - Create a
Booklist page using a function-based view and template. - Add a
Bookdetail page at/books/<id>/. - Implement a “Create Book” form using
ModelForm. - Implement “Update Book” and “Delete Book” views with confirmation page.
- Add pagination to the
Booklist page (10 per page). - Add search by title with
?q=query parameter (case-insensitive). - Add ordering by
published_datewith?sort=published_date. - Create an
Authormodel and addForeignKeyfromBooktoAuthor. - Show author name on book detail page; list author’s books on author detail page.
- Add a
ManyToManyFieldtagstoBookand create aTagmodel. - Build a page to filter books by tag.
- Add a
slugfield toBook; auto-generate it from title on save. - Create a URL pattern using slug instead of id for book detail.
- Add a custom admin list display with search and filters for
Book. - Add admin inline editing for related models (e.g.,
Chapterinline underBook). - Create a
Chaptermodel related toBookand show chapters on book detail. - Create a management command to import books from a JSON file.
- Create a management command to export all books to CSV.
- Create a middleware that adds a request ID to
requestand response headers. - Log each request method/path/status code with timing in middleware.
- Create a custom template filter to format currency for
price. - Create a custom template tag that renders “New” badge for recently published books.
- Implement user login/logout using Django auth templates.
- Protect Create/Update/Delete views with
login_required. - Add “staff only” restriction to Delete view.
- Create a user profile model with
OneToOneFieldtoUser. - Auto-create profile when a user is created (signals).
- Add a profile edit page for logged-in users.
- Add file upload field
cover_imagetoBookand configure media serving in dev. - Validate uploaded cover images by size and extension in the form.
- Add a “featured” boolean field and show featured books separately on homepage.
- Create a JSON endpoint
/api/books/returning list of books (no DRF). - Add a JSON endpoint
/api/books/<id>/returning a single book. - Implement basic error handling in JSON endpoints (404, validation errors).
- Add CSRF-protected form submission and confirm CSRF errors are handled.
- Add a contact form using
forms.Formand send an email using console email backend. - Implement a simple “like” feature:
BookLikemodel linking user+book, unique constraint. - Add a button to like/unlike a book; show like count.
- Prevent N+1 queries on the book list using
select_related/prefetch_related. - Add a sitemap-like page listing all URLs for books and authors.
- Add a base template with navbar and extend it in all pages.
- Implement custom 404 page template.
- Create unit tests for
Bookmodel (slug generation) and one view (list view 200). - Create tests for permission: anonymous user cannot access Create view.
- Create a fixture or factory to generate test books.
- Add environment-based settings:
DEBUG,ALLOWED_HOSTS,SECRET_KEYfrom env vars. - Configure static files and add one custom CSS file; verify it loads.
- Add a
Reviewmodel linked toBookand build a form to add a review on the detail page. - Add average rating calculation and show it on book list.