Answer the question
In order to leave comments, you need to log in
Is it possible to create dependent selects in django without reloading the page?
Good day!
The point is the following.
There are several MySQL tables linked via FK: Countries, Federal Districts, Regions, Cities.
#models.py
class Country(models.Model):
country_name = models.CharField(max_length=200, verbose_name='Страна')
def __unicode__(self):
return self.country_name
class Meta:
verbose_name = 'страна'
verbose_name_plural = 'Страны'
class District(models.Model):
district_name = models.CharField(max_length=100, verbose_name='Федеральный округ')
district_country = models.ForeignKey('Country', verbose_name='Подчинение страны')
def __unicode__(self):
return self.district_name
class Meta:
verbose_name = 'федеральный округ'
verbose_name_plural = 'Федеральные округа'
#views.py
def CabinetView(request, id):
cab = Cabinet.objects.all().filter(id=id) #Выводится список нужных кабинетов
cab_type = CabinetTypes.objects.all() #... их типов
country = Country.objects.all() #... список стран
district = District.objects.all() #... список фед. округов
region = Region.objects.all() #... список районов
city = City.objects.all() #...список городов
rendering = {
'cab': cab,
'cab_type': cab_type,
'country': country,
'district': district,
'region': region,
'city': city,
}
return render_to_response('cabinet.html', rendering)
<!-- cabinet.html -->
<tr>
<th>Страна</th>
<td>
<select name='cab_coutry' class='forinput_edit' id='select-1'>
{% for i in country %}
<option value='{{ i.id }}' required>{{ i.country_name }}</option>
{% endfor %}
</select>
</td>
</tr>
<tr>
<th>Федеральный округ</th>
<td>
<select name='cab_district' class='forinput_edit' id='select-2'>
{% for i in district %}
<option value='{{ i.id }}' required>{{ i.district_name }} ({{ i.district_sname }})</option>
{% endfor %}
</select>
</td>
</tr>
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