Django Paginator 无法正常工作,我该如何解决
Django Paginator doesnt work properly, how can i resolve
我在项目中使用的django分页器数据在排序时无法正常工作,例如,虽然我的记录中有日期为2021年的记录,但当我按日期排序时,它只对日期为2020年的记录进行排序,这是我的代码:
图片;
排序后;
通过筛选查找大约 2021 年的日期记录;
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 上市;
<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> Açıklama </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>
我在您的代码中没有看到任何按日期排序的信息。您可以尝试使用 gmembers_list = gunluk.objects.all().order_by("-date")
(或您在此模型上命名的任何日期字段)吗?
默认情况下,如果您不在 Model.Meta
class 上提供排序,并且在构建查询集时不提供排序,则排序可能会不稳定,具体取决于您的数据库。
您没有添加任何逻辑来告诉 django 如何从数据库中检索数据。您可以将 Meta class 添加到您的模型 class
class ModelName(models.Model):
....
datetime = models.DateTimeField(default=datetime.now)
class Meta:
ordering = ['-datetime']
def __unicode__(self):
return unicode(self.name)
或:
gunluk.objects.all().order_by("-date")
我在项目中使用的django分页器数据在排序时无法正常工作,例如,虽然我的记录中有日期为2021年的记录,但当我按日期排序时,它只对日期为2020年的记录进行排序,这是我的代码:
图片;
排序后;
通过筛选查找大约 2021 年的日期记录;
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 上市;
<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> Açıklama </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>
我在您的代码中没有看到任何按日期排序的信息。您可以尝试使用 gmembers_list = gunluk.objects.all().order_by("-date")
(或您在此模型上命名的任何日期字段)吗?
默认情况下,如果您不在 Model.Meta
class 上提供排序,并且在构建查询集时不提供排序,则排序可能会不稳定,具体取决于您的数据库。
您没有添加任何逻辑来告诉 django 如何从数据库中检索数据。您可以将 Meta class 添加到您的模型 class
class ModelName(models.Model):
....
datetime = models.DateTimeField(default=datetime.now)
class Meta:
ordering = ['-datetime']
def __unicode__(self):
return unicode(self.name)
或:
gunluk.objects.all().order_by("-date")