Urls.py
path('search', views.search, name='search'),
Listings/choices.py
bedroom_choices = {
'1':1,
'2':2,
'3':3,
'4':4,
'5':5,
'6':6,
'7':7,
'8':8,
'9':9,
'10':10
}
price_choices = {
'100000':'$100,000',
'200000':'$200,000',
'300000':'$300,000',
'400000':'$400,000',
'500000':'$500,000',
'600000':'$600,000',
'700000':'$700,000',
'800000':'$800,000',
'900000':'$900,000',
'1000000':'$1M+',
}
state_choices = {
'AK': 'Alaska',
'AL': 'Alabama',
'AR': 'Arkansas',
'AS': 'American Samoa',
'AZ': 'Arizona',
}
Templates/listings/search.html
Views.py
from listings.choices import price_choices, bedroom_choices, state_choices
def index(request):
listings =Listing.objects.order_by('-list_date').filter(is_published=True)[:3]
context = {
'listings' : listings,
'state_choices' : state_choices,
'price_choices' : price_choices,
'bedroom_choices' : bedroom_choices,
}
return render(request, 'pages/index.html', context)
def search(request):
queryset_list =Listing.objects.order_by('-list_date')
if 'keywords' in request.GET:
keywords = request.GET['keywords']
if keywords:
queryset_list = queryset_list.filter(description__icontains=keywords)
if 'city' in request.GET:
city = request.GET['city']
if city:
queryset_list = queryset_list.filter(city__iexact=city)
if 'state' in request.GET:
state = request.GET['state']
if state:
queryset_list = queryset_list.filter(state__iexact=state)
if 'bedrooms' in request.GET:
bedrooms = request.GET['bedrooms']
if bedrooms:
queryset_list = queryset_list.filter(bedrooms__lte=bedrooms)
if 'price' in request.GET:
price = request.GET['price']
if price:
queryset_list = queryset_list.filter(price__lte=price)
context = {
'listings' : queryset_list,
'state_choices' : state_choices,
'price_choices' : price_choices,
'bedroom_choices' : bedroom_choices,
}