Filtering and Searching Transactions

Let’s add filter options to your transaction list, so users can:

  • Filter by date range
  • Filter by category
  • Filter by transaction type
  • Search notes

1. Update Your View: Accept Query Parameters

We’ll use GET parameters for filtering.

“`python name=tracker/views.py
from django.db.models import Sum, Q
from .models import Transaction, Category

def transaction_list(request):
# Get filter params
category_id = request.GET.get(‘category’)
transaction_type = request.GET.get(‘transaction_type’)
start_date = request.GET.get(‘start_date’)
end_date = request.GET.get(‘end_date’)
search_query = request.GET.get(‘search’)

transactions = Transaction.objects.select_related('category').order_by('-date')

# Apply filters
if category_id:
    transactions = transactions.filter(category_id=category_id)
if transaction_type:
    transactions = transactions.filter(transaction_type=transaction_type)
if start_date:
    transactions = transactions.filter(date__gte=start_date)
if end_date:
    transactions = transactions.filter(date__lte=end_date)
if search_query:
    transactions = transactions.filter(notes__icontains=search_query)

# Summaries (same as before)
income = transactions.filter(transaction_type='IN').aggregate(total=Sum('amount'))['total'] or 0
expenses = transactions.filter(transaction_type='EX').aggregate(total=Sum('amount'))['total'] or 0
balance = income - expenses
category_totals = transactions.values('category__name').annotate(total=Sum('amount'))

# Send all categories for filter dropdown
categories = Category.objects.all()

return render(request, 'tracker/transaction_list.html', {
    'transactions': transactions,
    'income': income,
    'expenses': expenses,
    'balance': balance,
    'category_totals': category_totals,
    'categories': categories,
    'selected_category': category_id,
    'selected_type': transaction_type,
    'start_date': start_date,
    'end_date': end_date,
    'search_query': search_query,
})



---

### **2. Update Your Template: Add Filter Form**

Add this form above your transaction list in `transaction_list.html`:



````html
<form method="get">
    <label>Category:
        <select name="category">
            <option value="">All</option>
            {% for cat in categories %}
                <option value="{{ cat.id }}" {% ifselected_category == cat.id|stringformat:"s" %}selected{% endif %}>{{ cat.name }}</option>
            {% endfor %}
        </select>
    </label>
    <label>Type:
        <select name="transaction_type">
            <option value="">All</option>
            <option value="IN" {% if selected_type == "IN" %}selected{% endif %}>Income</option>
            <option value="EX" {% if selected_type == "EX" %}selected{% endif %}>Expense</option>
        </select>
    </label>
    <label>Start Date:<input type="date" name="start_date" value="{{ start_date }}"></label>
    <label>End Date:<input type="date" name="end_date" value="{{ end_date }}"></label>
    <label>Search Notes:<input type="text" name="search" value="{{ search_query }}"></label>
    <button type="submit">Filter</button>
    <a href="{% url 'transaction_list' %}">Clear</a>
</form>

Similar Posts