如何使用依赖的下拉列表Django + JS进行搜索?

时间:2019-05-09 19:35:52

标签: javascript json ajax django search

请告诉我,如果不困难,如何正确编写脚本,以便在选择区域时仅显示该区域的城市,而在选择城市时仅显示该城市的区域。我需要这个来搜索。

型号:

class Region(models.Model):
    name = models.CharField(max_length=100, verbose_name='Название области')

    def __str__(self):
        return self.name

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 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

views.py中的搜索功能:

def search(request):
    districts = District.objects.all()
    cities = City.objects.all()
    regions = Region.objects.all()
    queryset_list = Listing.objects.order_by('-list_date')

    if 'region' in request.GET:
        region_id = request.GET.get('region')
        cities = City.objects.filter(region_id=region_id).order_by('name')
        if region_id:
            queryset_list = queryset_list.filter(region_id=region_id)

    if 'city' in request.GET:
        city_id = request.GET.get('city')
        districts = District.objects.filter(city_id=city_id).order_by('name')
        if city_id:
            queryset_list = queryset_list.filter(city_id=city_id)

    if 'district' in request.GET:
        district_id = request.GET.get('district')
        if district_id:
            queryset_list = queryset_list.filter(district_id=district_id)

    context = {
        'districts': districts,
        'cities': cities,
        'regions': regions,
        'listings': queryset_list,
        'values': request.GET
    }
    return render(request, 'listings/search.html', context)

到目前为止,由于有了此功能,整个区域,城市和地区的列表都可以动态显示在模板中,搜索也可以进行,但必须使选择依赖。 search.html:

    <form action="{% url 'search' %}">    
               <div class="form-row">
                <div class="col-md-3 mb-3">                  
                  <label class="sr-only">Region</label>
                  <select name="region" class="form-control" id="id_region">
                    <option selected="true" disabled="disabled">Region</option>
                    {% for region in regions %}
                      <option value="{{ region.pk }}">{{ region.name }}</option>
                    {% endfor %}
                  </select>
                </div>
                <div class="col-md-3 mb-3">
                  <label class="sr-only">City</label>
                  <select name="city" class="form-control" id="id_city">
                    <option selected="true" disabled="disabled">City</option>
                      {% for city in cities %}
                        <option value="{{ city.pk }}">{{ city.name }}</option>
                      {% endfor %}
                  </select>
                </div>                
                <div class="col-md-3 mb-3">
                  <label class="sr-only">District</label>
                  <select name="district" class="form-control" id="id_district">
                    <option selected="true" disabled="disabled">District</option>
                      {% for district in districts %}
                        <option value="{{ district.pk }}">{{ district.name }}</option>
                      {% endfor %}
                  </select>
                </div>
              </div>
<button class="btn btn-secondary btn-block mt-4" type="submit">Search</button>
            </form>

我知道这是在JS上完成的,但是我是完全陌生的,所以,如果不难,请举个例子,在我的情况下脚本会是什么样?提前非常感谢!

1 个答案:

答案 0 :(得分:0)

您可以使用smart_selects软件包

示例:

* 5c3c4c0 2019-05-10 10:31:49 (HEAD -> devBranch1, origin/devBranch1) Merge remote branch 'devBranch1' into devBranch1
| * 60d9558 2019-05-09 10:31:49 (origin/devBranch2) modify
|/
* df49eb7 2019-05-08 10:31:49 (devBranch2) test