4

Probably I haven't yet understand how the urls.py works...but I don't know how to load the data with "bServerSide" of datatables... I think to have a problem with my urls.py. I use Django, datatables.js and this is my code:

main.html

<table cellpadding="0" cellspacing="0" border="0" id="example1">
    <thead>
        <tr><th>Name</th></tr>
    </thead>
    <tbody></tbody>
</table>

<script type="text/javascript" language="javascript" class="init">
     $(document).ready(function() {
         $('#example1').dataTable( {                 
             "bServerSide": true,
             "sAjaxSource": "main.html/getdata_json",
             "bProcessing": true,
         } );
     } );
 </script>

views.py

def myajaxview(request):
   report = []
   start = request.GET['iDisplayStart']
   length = request.GET['iDisplayLength']    
   query = name.objects.all() #or any kind of queryset       
   query = query[start:start+length]
   for q in query:
      report.append(json.dumps(q.nome_struttura))    
   json = json.dumps(report)
   return HttpResponse(json, content_type='application/json')

urls.py

  urlpatterns = i18n_patterns('',
      ...
      url(r'^getdata_json$', 'views.myajaxview'),
      ... 

I don't know where is the error. Can you help me please?

2
  • Why have you got "main.html" in your sAjaxSource value? Commented Sep 22, 2014 at 13:31
  • Because if I don't put "main.html" in sAjaxSource I get "Page not found (404)". Commented Sep 22, 2014 at 13:35

1 Answer 1

3

You should try to avoid writing urls in more than one place (follow the DRY principle), you can give a name to your url, like this:

url(r'^getdata_json$', 'views.myajaxview', name='getdata_json')

Then in HTML use Django builtin template tag url to retrieve the view url:

<table cellpadding="0" cellspacing="0" border="0" id="example1"
    data-url="{% url 'getdata_json' %}">

Then in JS you can use jQuery data method to retrieve the URL (instead of hardcoding it):

<script type="text/javascript" language="javascript" class="init">
 $(document).ready(function() {
     $('#example1').dataTable( {                 
         "bServerSide": true,
         "sAjaxSource": $(this).data('url'),
         "bProcessing": true,
     } );
 } );
 </script>

As a side note, you could just append q.nome_struttura to your report array, using json.dumps on the final array, like this:

for q in query:
    report.append(q.nome_struttura)
json = json.dumps(report)

In general, when you are using AJAX requests you should use a browser developer tool (like Firebug for Firefox or Chrome Developer tools) to see exactly what data are passed to the server and how the response goes.

For your information there is a third party Django app meant exactly for your use case: that is integrating Django with jQuery Datatables with server side processing.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your respond but I get ever page not found.. $(this).data('url') in undefined.
I replace $(this).data('url') width {% url 'getdata_json' %} and I get the data! How can I show the data in the table now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.