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

141
Views
Obtenga datos de la base de datos y proyéctelos en una tabla de datos usando django/ajax

Recientemente aprendí Django/ajax/datatables. Puedo proyectar mis datos usando un bucle {%for%} y estoy tratando de hacer lo mismo con llamadas ajax.

Mi vista:

 def is_ajax(request): return request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest' def getfromServer(request): if is_ajax(request=request) and request.method == "GET": books= Book.objects.all() bookserial = serializers.serialize('json', books) return JsonResponse(bookserial, safe=False) return JsonResponse({'message':'Wrong validation'})

índice.html

 <div class="container"> <table id="books" class="display" style="width:100%"> <thead> <tr> <th>Book</th> <th>Author</th> <th>Genre</th> <th>Date Publishedd</th> <th>Copies</th> </tr> </thead> </table> </div> <script> $(document).ready(function() { $('#books').DataTable({ ajax: { type: "GET", datatype : 'json', url: 'views/getfromServer', }, columns: [ { data: 'name' }, { data: 'author' }, { data: 'genre' }, { data: 'pub_date' }, { data: 'copies' }, ] }); </script>

Estoy bastante seguro de que funciona de esta manera, pero no puedo entenderlo.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

jQuery DataTable is a powerful and smart HTML table enhancing plugin provided by jQuery JavaScript library

Por lo tanto, no tiene sentido colocar una solicitud ajax dentro del método .DataTable() debe realizar la solicitud ajax:

 $.ajax({ type: "GET", datatype : 'json', url: 'views/getfromServer', success: function (result) { // result is the response you get from the server if successful // Use the data in result to write the values to your html table respectively here } error: function (err) { // handle error } })
about 4 years ago · Juan Pablo Isaza Report

0

Eso es lo que se me ocurrió, pero todavía no parece funcionar. Todo lo que obtengo es una mesa vacía.

 $.ajax({ type: "GET", datatype : 'json', url: "views/getfromServer", // "{% url 'index' %}" success: function (response) { var instant = JSON.parse(response[books]); for book in books { var fields= instant[book]["fields"]; $("#books tbody").prepend( `<tr> <td>${fields["name"]||""}</td> <td>${fields["author"]||""}</td> <td>${fields["genre"]||""}</td> <td>${fields["pub_date"]||""}</td> <td>${fields["copies"]||""}</td> </tr>` ) } }, error: function (response) { alert(response["responseJSON"]["error"]); } }) $(document).ready(function() { $('#books').DataTable();
about 4 years ago · Juan Pablo Isaza 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!