2012-08-07 212 views
0

我使用的模板显示的表格中有多于2000行。但是在这里实现django分页会导致它太慢,因为数据太多。什么是最佳方式关于这一点。或者我怎样才能使页面每页加载200行,并使页面更快。 我怎样才能使网页加载速度更快Django对大量数据进行分页

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




     {% autopaginate response_dict.taggeddata 100 %} 
      <div align="right">{% paginate %}</div> 
    <form action="https://stackoverflow.com/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> 

回答

-2

我在Django使用分页多达400万个的数据集。工作没有任何问题PERF ...

+0

ü不明白problem.For每一页,如果查询400万个数据rows.Then会有一个问题:) – Rajeev 2012-08-07 18:40:27

+0

所以我错过了什么?也许你可以很快解释为什么我每次加载分页页面时都要经过4m。我有点困惑 – patroqueeet 2012-08-07 19:02:38

+0

这就是我beleive是发生在我的情况。每次我到了下一页我看到一个请求是对服务器和所有2000行被提取每次。如果你看到我的分页代码即, %autopaginate response_dict.taggeddata 100%}

{% paginate %}
Rajeev 2012-08-08 06:01:56

0

基于类的使用意见,paginate_by:看django.views.generic.list.BaseListView。 此外,如果性能很重要,请使用ajax并使用json将数据传递到前端。 此外,我会建议你使用Model.objects.values_list()

事情是这样的:

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)