Django. How to do a dependent search on the website?

Hi all. I need to make, when you select region, in the second box to show only city of this region, and the choice of the city, in the third field appears only areas of this city.

5c992a339323e281497792.png

Main model:
class Listing(models.Model):
 realtor = models.ForeignKey(Realtor, on_delete=models.CASCADE, verbose_name='Realtor')
 category = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name='Category')
 region = models.ForeignKey(Region, on_delete=models.CASCADE, verbose_name='Area')
 city = models.ForeignKey(City, on_delete=models.CASCADE, verbose_name='City')
 district = models.ForeignKey(District, on_delete=models.CASCADE, verbose_name='District')

Administrative model:
class Region(models.Model):
 name = models.CharField(max_length=100, verbose_name='Name of field')

 def __str__(self):
 return self.name

 class Meta:
 verbose_name = 'Area'
 verbose_name_plural = 'Region'

class City(models.Model):
 region = models.ForeignKey(Region, on_delete=models.CASCADE, verbose_name='Area')
 name = models.CharField(max_length=100, verbose_name='city Name')

 def __str__(self):
 return self.name

 class Meta:
 verbose_name = 'City'
 verbose_name_plural = 'Cities'

class District(models.Model):
 city = models.ForeignKey(City, on_delete=models.CASCADE, verbose_name='City')
 name = models.CharField(max_length=100, verbose_name='Name of district')

 def __str__(self):
 return self.name

 class Meta:
 verbose_name = 'District'
 verbose_name_plural = 'Areas'

views.py
def search(request):
 queryset_list = Listing.objects.order_by('-list_date')

 # Stage
 if 'stage' in the request.GET:
 stage = request.GET['stage']
 if stage:
 queryset_list = queryset_list.filter(stage__iexact=stage)

 # Rooms
 if 'rooms' in the request.GET:
 rooms = request.GET['rooms']
 if rooms:
 queryset_list = queryset_list.filter(rooms__iexact=rooms)

 # Price
 if 'price' in the 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)

Here is a template:
<div class="form-row">
 <div class="col-md-4 mb-3"> 
 <label class="sr-only">Area</label>
 <select name="region" class="form-control">
 <option selected="true" disabled="disabled">Area</option>
 <option value=""></option>
</select>
</div>
 <div class="col-md-4 mb-3">
 <label class="sr-only">City</label>
 <select name="city" class="form-control">
 <option selected="true" disabled="disabled">City</option>
 <option value=""></option>
</select>
 </div> 
 <div class="col-md-4 mb-3">
 <label class="sr-only">District</label>
 <select name="district" class="form-control">
 <option selected="true" disabled="disabled">District</option>
 <option value=""></option>
</select>
</div>
 </div>

Prompt, please, who knows. Thank you.
March 19th 20 at 08:33
1 answer
March 19th 20 at 08:35

Find more questions by tags Django