0
0
Djangoframework~10 mins

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

Choose your learning style9 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.