[Answered ]-Django ajax url not reading

1👍

You need to send the data in your ajax request like so:

$.ajax({
    url: "{% url 'accounts:prog-json'%}",
    data: {
          'department': selectedDept
        },
    success: function(response){
        console.log(response)
        },
        error: function(error){
        console.log(error)
    }
})

And then on your view you can access that argument from the request:

def get_json_prog_data(request):
    selected_department = request.GET.get('department')
    obj_prog = list(Program.objects.filter(department__name=selected_department).values())
    return JsonResponse({'data':obj_prog})

I dont believe you need the parameter in your url, so you should be able to just remove that as we are sending the data in the ajax request.

path('prog-json/', get_json_prog_data, name='prog-json'),

Leave a comment