简体   繁体   中英

Django Paginator doesnt work properly, how can i resolve

the django paginator data I use in my project does not work properly in sorting, for example, although I have records dated 2021 in my records, when I sort by date, it only sorts records dated 2020, here is my codes:

images;

在此处输入图片说明

after sorting;

在此处输入图片说明

about 2021 dated records finding by filtering;

在此处输入图片说明

views.py;

gmembers_list = gunluk.objects.all()
paginator = Paginator(gmembers_list, 1000000000000000)
page = request.GET.get('page')

try:
    gmembers = paginator.page(page)
except PageNotAnInteger:
    gmembers = paginator.page(1)
except EmptyPage:
    gmembers = paginator.page(paginator.num_pages)
return render(request, 'gunlukistakibi.html', {'gmembers': gmembers})a

html for listing;

 <div class="card mb-3">
            <div class="card-header">
                <i class="fas fa-table"></i>
                Günlük İş Takibi
                <a  class="btn btn-sm btn-success" href="{% url 'gcreate' %}" style="padding: 8px; float: right; background-color: green; color: white;">EKLE</a>
            </div>
            <div class="card-body">
                <div class="table-responsive ">
                    <table class="table table-striped table-bordered text-center dataTable no-footer align-center align-middle " id="dataTable" width="100%" cellspacing="0">
                        <thead>
                        <tr>
                            <th>Tarih</th>
                            <th>Ad Soyad</th>                       
                            <th>Vardiya</th>
                            <th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Açıklama&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
                            <th>İncident</th>
                            <th>Alınan</br>Aksiyon</th>                             
                            <th>Ulaşılamayan</br>Ekip</th>
                            <th>Ulaşılamayan</br>Bilgisi</th>
                            <th>Action</th>                               
                        </tr>
                        </thead>
                        <tbody>
                        {% for gmember in gmembers %}
                            <tr>
                                <td>{{gmember.tarih|date:"d-m-Y"}}</td>
                                <td>{%if gmember.adsoyad2 == null%} {{ gmember.adsoyad}} {%else%} {{ gmember.adsoyad}} - {{ gmember.adsoyad2}}  {%endif%}</td>
                                <td>{{ gmember.vardiya }}</td>
                                <td>{{ gmember.aciklama }}</td>
                                <td>{{ gmember.incident }}</td>
                                <td>{{ gmember.alinanaksiyon }}</td>
                                <td>{{ gmember.ulasilmayanekip }}</td>
                                <td>{{ gmember.ulasilmayanbilgisi }}</td>                               
                                <td>
                                    <a class="btn btn-sm btn-warning" href="gedit/{{ gmember.id }}"> <span class="fa fa-edit"></span> </a>
                                    <a class="btn btn-sm btn-danger" href="gdelete/{{ gmember.id }}"><span class="fa fa-trash"></span> </a>
                                </td>
                            </tr>

I don't see any sorting by date in your code. Can you try with gmembers_list = gunluk.objects.all().order_by("-date") (or whatever your date field on this model is named)?

By default, if you don't provide an ordering on the Model.Meta class, and no ordering when building your queryset, the sorting can be unstable depending on your database.

You did not add any logic to tell django how to retrive the data from db. You either add Meta class to your model class

class ModelName(models.Model):
    ....
    datetime = models.DateTimeField(default=datetime.now)
    class Meta:
        ordering = ['-datetime']      
    def __unicode__(self):
        return unicode(self.name)

or:

  gunluk.objects.all().order_by("-date")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM