0
0
Djangoframework~30 mins

Search and ordering in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Search and Ordering in Django
📖 Scenario: You are building a simple Django app to display a list of books. Users want to search for books by title and order the results by price.
🎯 Goal: Create a Django view that filters books by a search term in the title and orders the results by price.
📋 What You'll Learn
Create a list of books as Django model instances
Add a search term variable to filter books by title
Use Django QuerySet filtering and ordering
Return the filtered and ordered books in the view
💡 Why This Matters
🌍 Real World
Filtering and ordering data is common in web apps to help users find and sort items like products or articles.
💼 Career
Understanding how to filter and order data is essential for backend and full-stack developers working with databases and APIs.
Progress0 / 4 steps
1
Create the initial list of books
Create a list called books with these dictionaries exactly: {'title': 'Django Basics', 'price': 30}, {'title': 'Advanced Django', 'price': 45}, {'title': 'Python for Beginners', 'price': 25}, {'title': 'Django and REST', 'price': 40}
Django
Need a hint?

Use a list of dictionaries with keys 'title' and 'price'.

2
Add a search term variable
Create a variable called search_term and set it to the string 'Django'
Django
Need a hint?

Set search_term to the exact string 'Django'.

3
Filter books by search term in title
Create a list called filtered_books that includes only books from books where search_term is in the book's title (case sensitive). Use a list comprehension.
Django
Need a hint?

Use a list comprehension with if search_term in book['title'].

4
Order filtered books by price
Create a list called ordered_books by sorting filtered_books by the price key in ascending order using the sorted() function and a lambda function.
Django
Need a hint?

Use sorted(filtered_books, key=lambda book: book['price']) to order by price.