B
B
blackbb2018-08-18 13:44:31
Django
blackbb, 2018-08-18 13:44:31

How to organize list of cities depending on country selection in select?

Hello. There is a form in which there are two selects, the first is the choice of the country, the second is the choice of the city. It is necessary that when choosing a certain country, only the cities of this country should be in the second select. I understand that it is necessary to organize using ajax, but there is not enough knowledge.

<select class="custom-select" name="country_from" id="country_from">
  <option selected value=''>{% trans 'Russia' %}</option>
  {% for item in country_list %}
  <option value="{{item.id}}">{{item}}</option>
  {% endfor %}
</select>
  <div class="select_wrap">
  <select name="city_from" id="id_city_from">
  <option selected value=''>{% trans 'All' %}</option>
  {% for item in city_list %}
  <option value="{{item.id}}">{{item}}</option>
  {% endfor %}
        </div>

Script
$(document).ready(function() {
    $("#country_from").change(function(){
        $.ajax({
            type: 'GET',
            url: '/city/search',
            data:  {country:$('#country_from option:selected').val()}
        });
    });
});

urls.py
urlpatterns = [
    path('search/', citysearch, name='city_search'),
]

Help me write a view that will return a list in the format needed for processing and a script that will insert this list into the second select.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question