Bird
Raised Fist0
Djangoframework~10 mins

Search and filter options in Django - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Search and filter options
User enters search/filter criteria
↓
View receives request with criteria
↓
Query database with filters
↓
Get filtered results
↓
Render results in template
↓
User sees filtered list
User inputs criteria, view filters database query, then renders filtered results.
Execution Sample
Django
def product_list(request):
    query = request.GET.get('q', '')
    products = Product.objects.filter(name__icontains=query)
    return render(request, 'products.html', {'products': products})
Filters products by name containing the search query and shows results.
Execution Table
StepActionInput/ConditionQuery ResultRendered Output
1Receive requestGET parameter q='phone'N/AN/A
2Extract queryquery = 'phone'N/AN/A
3Filter productsname__icontains='phone'Products with 'phone' in nameN/A
4Render templateproducts filteredN/AList of products with 'phone' shown
5User views pageN/AN/AFiltered product list displayed
💡 Rendering completes after filtering products by search query.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
query'' (empty)'phone''phone''phone'
productsN/AN/AQuerySet filtered by 'phone'Same filtered QuerySet
Key Moments - 2 Insights
Why do we use name__icontains instead of just name?
Because name__icontains does a case-insensitive search inside the name field, matching partial text. See execution_table step 3 where filtering happens.
What happens if the user does not enter a search query?
The query variable is empty string '', so filter returns all products containing '', which means all products. This is shown in variable_tracker where query starts as ''.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'query' after step 2?
ANone
B'' (empty string)
C'phone'
D'products'
💡 Hint
Check the 'Input/Condition' column at step 2 in execution_table.
At which step does the database get queried with the filter?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for 'Filter products' action in execution_table.
If the user enters no search query, what will the filter return?
AAll products
BNo products
CError
DOnly products with empty names
💡 Hint
Refer to variable_tracker for 'query' initial value and key_moments explanation.
Concept Snapshot
Search and filter in Django:
- Get user input from request.GET
- Use filter() with __icontains for case-insensitive partial match
- Pass filtered QuerySet to template
- Empty query returns all results
- Render filtered list for user
Full Transcript
This example shows how Django handles search and filter options. The user types a search term, which the view gets from the request's GET parameters. The view uses the filter method with name__icontains to find products whose names include the search term, ignoring case. The filtered products are sent to the template to display. If the search term is empty, all products are shown. This flow ensures users see only items matching their search.

Practice

(1/5)
1. What does the Django ORM method filter() do when used with icontains in a query?
easy
A. It returns records where the specified field contains the given substring, case-insensitive.
B. It deletes records that contain the given substring in the specified field.
C. It updates records to include the given substring in the specified field.
D. It sorts records alphabetically based on the specified field.

Solution

  1. Step 1: Understand filter() usage

    The filter() method in Django ORM is used to select records that match certain conditions.
  2. Step 2: Understand icontains lookup

    icontains checks if a field contains a substring, ignoring case differences.
  3. Final Answer:

    It returns records where the specified field contains the given substring, case-insensitive. -> Option A
  4. Quick Check:

    filter() + icontains = case-insensitive substring search [OK]
Hint: Remember: filter() narrows results; icontains ignores case [OK]
Common Mistakes:
  • Confusing filter() with delete()
  • Thinking icontains is case-sensitive
  • Assuming filter() modifies data
  • Mixing filter() with sorting methods
2. Which of the following is the correct syntax to filter a Django model Book by title containing the word 'django' ignoring case?
easy
A. Book.objects.get(title__icontains='django')
B. Book.objects.filter(title.contains='django')
C. Book.objects.filter(title__contains='django')
D. Book.objects.filter(title__icontains='django')

Solution

  1. Step 1: Identify correct field lookup syntax

    Django uses double underscores to separate field and lookup, so title__icontains is correct.
  2. Step 2: Choose correct method for filtering multiple records

    filter() returns a queryset; get() returns a single object and may error if multiple matches.
  3. Final Answer:

    Book.objects.filter(title__icontains='django') -> Option D
  4. Quick Check:

    filter() + __icontains syntax = Book.objects.filter(title__icontains='django') [OK]
Hint: Use double underscores for lookups and filter() for multiple results [OK]
Common Mistakes:
  • Using single underscore instead of double
  • Using get() when multiple results expected
  • Using contains instead of icontains for case-insensitive
  • Using incorrect method names like contains
3. Given the following Django view code snippet, what will books contain if the URL query string is ?search=python?
def book_list(request):
    query = request.GET.get('search', '')
    books = Book.objects.filter(title__icontains=query)
    return render(request, 'books.html', {'books': books})
medium
A. All Book objects with titles containing 'python' ignoring case.
B. All Book objects with titles exactly equal to 'python'.
C. An empty queryset because 'search' is not a valid field.
D. All Book objects regardless of title.

Solution

  1. Step 1: Extract query parameter from request

    The code gets the 'search' parameter from the URL query string, which is 'python'.
  2. Step 2: Apply filter with icontains lookup

    The filter uses title__icontains='python', so it returns books with titles containing 'python' ignoring case.
  3. Final Answer:

    All Book objects with titles containing 'python' ignoring case. -> Option A
  4. Quick Check:

    GET param + icontains filter = All Book objects with titles containing 'python' ignoring case. [OK]
Hint: GET params filter queryset with icontains for partial match [OK]
Common Mistakes:
  • Thinking filter matches exact text only
  • Assuming empty query returns all results
  • Confusing GET params with POST data
  • Believing filter ignores the query parameter
4. Identify the error in this Django view code for filtering Product objects by a search term from the URL:
def product_search(request):
    term = request.GET['search']
    results = Product.objects.filter(name_icontains=term)
    return render(request, 'search.html', {'results': results})
medium
A. Using request.GET['search'] causes a syntax error.
B. The field lookup should use double underscores: name__icontains.
C. The filter method should be get() instead of filter().
D. The render function is missing the request parameter.

Solution

  1. Step 1: Check field lookup syntax in filter()

    The code uses name_icontains, but Django requires double underscores between field and lookup: name__icontains.
  2. Step 2: Verify other parts of the code

    Using request.GET['search'] is valid but may raise KeyError if missing; render() usage is correct with request parameter.
  3. Final Answer:

    The field lookup should use double underscores: name__icontains. -> Option B
  4. Quick Check:

    Field lookup syntax requires double underscores [OK]
Hint: Use double underscores for lookups like __icontains [OK]
Common Mistakes:
  • Using single underscore in field lookups
  • Confusing filter() with get()
  • Forgetting to pass request to render()
  • Assuming request.GET[] never raises errors
5. You want to build a Django view that filters Article objects by a search term in the title and also filters by a minimum number of views provided by the user. Which of the following code snippets correctly applies both filters using values from request.GET?
hard
A. query = request.GET['q'] min_views = request.GET['min_views'] articles = Article.objects.filter(title__icontains=query).filter(views__lte=min_views)
B. query = request.GET.get('q') min_views = request.GET.get('min_views') articles = Article.objects.filter(title__contains=query, views__gt=min_views)
C. query = request.GET.get('q', '') min_views = int(request.GET.get('min_views', 0)) articles = Article.objects.filter(title__icontains=query, views__gte=min_views)
D. query = request.GET.get('q', '') min_views = int(request.GET.get('min_views', 0)) articles = Article.objects.filter(title__icontains=query).exclude(views__lte=min_views)

Solution

  1. Step 1: Extract and convert GET parameters safely

    Using get() with default values avoids errors. Converting min_views to int is necessary for numeric comparison.
  2. Step 2: Apply combined filters correctly

    Using filter() with multiple conditions joined by commas applies AND logic. title__icontains=query and views__gte=min_views correctly filter by title substring and minimum views.
  3. Final Answer:

    query = request.GET.get('q', '') min_views = int(request.GET.get('min_views', 0)) articles = Article.objects.filter(title__icontains=query, views__gte=min_views) -> Option C
  4. Quick Check:

    Safe GET + combined filter with AND = query = request.GET.get('q', '') min_views = int(request.GET.get('min_views', 0)) articles = Article.objects.filter(title__icontains=query, views__gte=min_views) [OK]
Hint: Use filter() with multiple conditions and convert GET values properly [OK]
Common Mistakes:
  • Not converting min_views to int before filtering
  • Using filter() then filter() with conflicting conditions
  • Using contains instead of icontains for case-insensitive search
  • Using exclude() incorrectly to simulate greater than or equal
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard