0

I am using a template for displaying a table which has more than 2000 rows.But implementing django pagination in this makes it too slow as the data is too much.What is the best way to go about this.Or how can i make the pagination load 200 rows per page and make the page faster. How can i make the page load faster

   {% extends "base/admin_base.html" %}
   {% load pagination_tags %}




         {% autopaginate response_dict.taggeddata 100 %}
           <div align="right">{% paginate %}</div>
  <form action="/users/saveuser/" method="post">{% csrf_token %}
  <b>
        <table>

         <tr><td><font>*</font>Select Category group for tagging</td><td> 
         <select id="cgroup" onchange="getcategory('1');">
         <!--<option value="-1">Select Category group</option>   -->
         {% for group in response_dict.categorygroup %}
                  <option value="{{group.id}}">{{group.name}}</option> 
         {% endfor %}
         </select>  
         </td></tr>

        </table>
        </b>
        <table  id="box-table-a">
        <colgroup>
        <col class="vzebra-odd">
        <col class="vzebra-even">
        <col class="vzebra-odd">
        <col class="vzebra-even">
        <col class="vzebra-odd">
        <col class="vzebra-even">
        <col class="vzebra-odd">
        <col class="vzebra-even">
        </colgroup>
        <thead>
         <tr><th id="vzebra-comedy" scope="col">Field1</th><th id="vzebra-adventure" scope="col">Field2</th><th id="vzebra-comedy" scope="col">Field3</th><th id="vzebra-adventure" scope="col">Field4</th><th id="vzebra-comedy" scope="col">Field5</th><th id="vzebra-adventure" scope="col">Field6</th><th id="vzebra-comedy" scope="col">Tag</th><th id="vzebra-adventure" scope="col">Actions</th><thead></tr>
        <tbody>  
         {% for td in response_dict.taggeddata %}
           <tr id="{{td.id}}">
           <td width="20%">{{td.field1}}</td>
           <td>{{td.field2}}</td>
           <td>{{td.field3}}</td>
           <td>{{td.field4}}</td>
           <td>{{td.field5}}</td>
           <td>{{td.field6}}</td>
           <td class="tg">Select category</td>
           <td ><img src="/media/img/cancel.png" alt="delete" height="20" width="20" onclick="delete_row('{{td.id}}')"></td>
           </tr>
         {% endfor %}
        </tbody>
        </table>
         <input type="button" value="Add" id="addbtn" onclick="validate();"/>

  </form>
Rajeev
  • 44,985
  • 76
  • 186
  • 285

2 Answers2

0

Use class-based views, paginate_by : see django.views.generic.list.BaseListView . Besides, if performance is important use ajax and pass data to a front end with json. Also I would recommend you to use Model.objects.values_list()

Something like this :

class MovieLandingJSON(JSONResponseMixin, BaseListView):
    """
    Movie landing page movie covers in json format.
    """
    paginate_by = 5
    context_object_name = 'movies'
    queryset = Movie.objects.all()
    values_list = ['id', 'title', 'slug']

    def get_queryset(self):
    """
    Filters  queryset on request
    """
        queryset = super(MovieLandingJSON, self).get_queryset()

        if self.values_list:
            queryset = queryset.values_list(*self.values_list)
        return queryset


from django.core.serializers import serialize
from django.utils.simplejson import dumps, loads, JSONEncoder
from django.db.models.query import QuerySet
from django.utils.functional import curry


class DjangoJSONEncoder(JSONEncoder):
    def default(self, obj):
        if isinstance(obj, QuerySet):
            # `default` must return a python serializable
            # structure, the easiest way is to load the JSON
            # string produced by `serialize` and return it
            return loads(serialize('json', obj))
        return JSONEncoder.default(self,obj)

dumps = curry(dumps, cls=DjangoJSONEncoder)


class JSONResponseMixin(object):
    """
    A mixin that can be used to render a JSON response.
    """
    response_class = HttpResponse

    def render_to_response(self, context, **response_kwargs):
        """
        Returns a JSON response, transforming QuerySet to json.
        """
        response_kwargs['content_type'] = 'application/json'
        return self.response_class(
            self.convert_queryset_to_json(context['object_list']),
            **response_kwargs
        )

    def convert_queryset_to_json(self, queryset):
        if self.values_list:
            return simplejson.dumps(list(queryset))
        else:
            return dumps(queryset)
-1

I use pagination in Django with up to 4 million data sets. works without any perf problems...

patroqueeet
  • 764
  • 1
  • 8
  • 19
  • u did not understand the problem.For every page if it queries the 4 million data rows.Then there would be a problem :) – Rajeev Aug 07 '12 at 18:40
  • So what did I miss? maybe you can explain quickly why I should go through the 4m each time a paginated page is loaded. i'm little confused – patroqueeet Aug 07 '12 at 19:02
  • Thats what i beleive is happening in my case.Every time i got to next page i see that a request is made to the server and all 2000 rows are fetched each time.If you see my pagination code i.e,% autopaginate response_dict.taggeddata 100 %}
    {% paginate %}
    – Rajeev Aug 08 '12 at 06:01
  • I don't see why this should happen. But we're guessing - which never helps. HAve you installed django debug toolbar? If not, pls get it and check the queries issued as well as the time spend per module. Once we have proven facts, we can work out the solution. – patroqueeet Aug 08 '12 at 08:41
  • I just checked your code again: you're using django-pagination? if I'm right: this is a depecated project of Django folks. I'd recommend to use the core feature instead... https://docs.djangoproject.com/en/dev/topics/pagination if you still want to got with dj-pag, follow my above comment first – patroqueeet Aug 08 '12 at 08:51
  • :Thanks i will go with ur suggestion of django pagination it really solved the issue for me.. – Rajeev Aug 08 '12 at 11:26