Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

453
Views
Django ajax url no lee

Tengo un archivo ajax que llama a la url de urls.py que obtiene los datos json de views.py pero cuando ejecuto el servidor simplemente muestra Not Found:/dept-json/

Logré que funcionara usando el fragmento de django {% url 'accounts:dept-json'%}

Sin embargo, no quiero esto, ya que necesito obtener y pasar la variable de los dept-json data a otra URL de ajax, que es:

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

He leído aquí que no puedes pasar variables en ajax como {% url 'accounts:prog-json' department%}

¿Cómo me las arreglo para hacer esto?

aquí está mi código:

principal.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'),

vistas.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 answers
Answer question

0

Debe enviar los datos en su solicitud ajax así:

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

Y luego, en su vista, puede acceder a ese argumento desde la solicitud:

 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})

No creo que necesite el parámetro en su URL, por lo que debería poder eliminarlo ya que estamos enviando los datos en la solicitud ajax.

 path('prog-json/', get_json_prog_data, name='prog-json'),
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!