I have 2 selects, one division and the other group. When the division is selected it has to bring in the second only the groups that are part of that division. In the url variable that takes json I called python according to my view, but instead of that 4 it has to be the id of the one that was previously selected. In this line: var url="{% url 'signup: all_json' 4%}";
<script>
$( document ).ready(function() {
$("select#id_divisao").change(function() {
if ($(this).val() == '') {
$("select#id_grupos").html("<option>Selecione uma divisão</option>");
$("select#id_grupos").attr('disabled', true);
} else {
var url = "{% url 'cadastro:all_json' 4 %}" ;
$.getJSON(url, function(grupos) {
var options = '<option value="">Select uma divisão</option>';
for (var i = 0; i < grupos.length; i++) {
options += '<option value="' + grupos[i].pk + '">' + grupos[i].fields.grupo + '</option>';
}
$("select#id_grupos").html(options);
$("select#iid_grupos option:first").attr('selected', 'selected');
});
}
});
});
</script>
views.py:
def all_json_grupos(request, idDivisao):
json_grupos = serializers.serialize("json", Grupo.objects.filter(divisao_id=idDivisao))
return HttpResponse(json_grupos)
I enter through the url and it shows me the groups correctly according to the division that I put, I just need to make it work in select