S
S
Stanislav Konovalov2019-03-25 22:24:48
Django
Stanislav Konovalov, 2019-03-25 22:24:48

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.
5c992a339323e281497792.png
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='Район')

Administration Models:
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 = 'Районы'

views.py
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)

Here is the template:
<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>

Tell me, please, who knows. Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor, 2019-03-25
@Igorello74

Same answer as here: https://toster.ru/answer?answer_id=1364695

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question