假设你正在谈论一系列的选择框这样做:
创建两个观点,其中一个返回一个包含Departamentos一个给定区域的响应。其他不相同,但对于Municipios在阿雅
# views.py
from django.core import serializers
def departamentos_por_region(request, region_id):
region = get_object_or_404(Region, id=region_id)
departamentos = Departamento.objects.filter(region=region)
return render_to_reponse("format_as_option_list.html",
{'departamentos': departamentos})
def municipios_por_departamento(request, departamento_id):
# basically the same as above
我假设您填写的初始页面视图中的地区选择框,所以不需要作特殊视图。
模板应该格式化为html选项列表。
假设那么在初始页面视图中的HTML看起来像:
<select id='regions'>
<option value='1'>Region 1</option>
<option value='2'>Region 2</option>
</select>
<select id='departamentos'>
</select>
<select id='municipios'>
</select>
你会使用一些JavaScript等(jQuery的):
// this isn't tested code and likely contains an error or two
$('#regions').change(function(){
// Region has changed, so reset Departamentos and Municipios
$('#departamentos').html("")
$('#municipios').html("")
// now update the departamentos
$.get('/ajax/departamentos_por_region/' + $('#regions').val(),
function(data) {
('#departamentos').html(data)
};
);
});
执行相同的Municipios为为Departamentos。
您可能还想在停用字段没有可用选项时处理这些问题,并处理没有退回或离开的情况。