Answer the question
In order to leave comments, you need to log in
Django. How to make a dependent search on the site?
Hello. I need to make it so that when you select an area, the second field displays the cities of only this region, and when you select a city, the third field displays only the districts of this city.
Main model:
class Listing(models.Model):
realtor = models.ForeignKey(Realtor, on_delete=models.CASCADE, verbose_name='Риелтор')
category = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name='Категория')
region = models.ForeignKey(Region, on_delete=models.CASCADE, verbose_name='Область')
city = models.ForeignKey(City, on_delete=models.CASCADE, verbose_name='Город')
district = models.ForeignKey(District, on_delete=models.CASCADE, verbose_name='Район')
class Region(models.Model):
name = models.CharField(max_length=100, verbose_name='Название области')
def __str__(self):
return self.name
class Meta:
verbose_name = 'Область'
verbose_name_plural = 'Области'
class City(models.Model):
region = models.ForeignKey(Region, on_delete=models.CASCADE, verbose_name='Область')
name = models.CharField(max_length=100, verbose_name='Название города')
def __str__(self):
return self.name
class Meta:
verbose_name = 'Город'
verbose_name_plural = 'Города'
class District(models.Model):
city = models.ForeignKey(City, on_delete=models.CASCADE, verbose_name='Город')
name = models.CharField(max_length=100, verbose_name='Название района')
def __str__(self):
return self.name
class Meta:
verbose_name = 'Район'
verbose_name_plural = 'Районы'
def search(request):
queryset_list = Listing.objects.order_by('-list_date')
# Stage
if 'stage' in request.GET:
stage = request.GET['stage']
if stage:
queryset_list = queryset_list.filter(stage__iexact=stage)
# Rooms
if 'rooms' in request.GET:
rooms = request.GET['rooms']
if rooms:
queryset_list = queryset_list.filter(rooms__iexact=rooms)
# Price
if 'price' in request.GET:
price = request.GET['price']
if price:
queryset_list = queryset_list.filter(price__iexact=price)
# Keywords
if 'keywords' in request.GET:
keywords = request.GET['keywords']
if keywords:
queryset_list = queryset_list.filter(description__icontains=keywords)
context = {
'price_choices': price_choices,
'room_choices': room_choices,
'stage_choices': stage_choices,
'listings': queryset_list,
'values': request.GET
}
return render(request, 'listings/search.html', context)
<div class="form-row">
<div class="col-md-4 mb-3">
<label class="sr-only">Область</label>
<select name="region" class="form-control">
<option selected="true" disabled="disabled">Область</option>
<option value=""></option>
</select>
</div>
<div class="col-md-4 mb-3">
<label class="sr-only">Город</label>
<select name="city" class="form-control">
<option selected="true" disabled="disabled">Город</option>
<option value=""></option>
</select>
</div>
<div class="col-md-4 mb-3">
<label class="sr-only">Район</label>
<select name="district" class="form-control">
<option selected="true" disabled="disabled">Район</option>
<option value=""></option>
</select>
</div>
</div>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question