Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

454
Vistas
Django ajax url not reading

I have an ajax file which calls the url from the urls.py which gets the json data from the views.py but when I run the server it just shows Not Found:/dept-json/

I did manage to get it to work using the django snippet {% url 'accounts:dept-json'%}

However I do not want this since I need to get and pass the variable from the dept-json data to another ajax url which is:

url:`/prog-data/${selectedDept}/`

I have read here that you can't pass variables in ajax like {% url 'accounts:prog-json' department%}

how do I manage to do this?

here is my code:

main.js

//First ajax
$.ajax({
    type: 'GET',
    url: "/dept-json/",
//    url: "{% url 'accounts:dept-json'%}",
    success: function(response){
        console.log(response)
        },
        error: function(error){
        console.log(error)
    }
})
//Second ajax
$.ajax({
    type: 'GET',
    url: `/prog-data/${selectedDept}/`,
    success: function(response){
        console.log(response)
        },
        error: function(error){
        console.log(error)
    }
})

urls.py

    path('dept-json/', get_json_dept_data, name='dept-json'),
    path('prog-json/<str:department>/', get_json_prog_data, name='prog-json'),

views.py

def get_json_dept_data(request):
    qs_val = list(Department.objects.values())
    return JsonResponse({'data':qs_val})

def get_json_prog_data(request, *args, **kwargs):
    selected_department = kwargs.get('department')
    obj_prog = list(Program.objects.filter(department__name=selected_department).values())
    return JsonResponse({'data':obj_prog})
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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

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

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

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

I don't think you need the parameter in your URL, so you should be able to remove it since we're sending the data in the ajax request.

 path('prog-json/', get_json_prog_data, name='prog-json'),
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda