我列出了具有相似属性的医生。我想根据他们喜欢的数量按降序在我的模板中列出它们。
这是我需要展示的模板
{% for doc in doctors %}
<h2><a href="/docprofile/{{doc.id}}/">{{doc.name}}</a></h2>
<h3> {{doc.specialization}}</h3>
<h4> {{doc.clinic.name}}</h4>
<h4> {{doc.clinic.address}}</h4>
<div class="like">
<img class="like" src="/static/meddy1/images/like.png">
<p>{{doc.likes}}</p>
</div>
{% endfor %}
</div>
</div>
这是我的views.py
@csrf_exempt
def doclistings(request):
d = getVariables(request)
doctors = Doctor.objects.all()
paginator = Paginator(doctors, 20) #Show 20 doctors per page
page = page = request.GET.get('page')
try:
doctors = paginator.page(page)
except PageNotAnInteger:
doctors = paginator.page(1)
except EmptyPage:
doctors = paginator.page(paginator.num_pages)
d['doctors'] = doctors
d['paginator'] = paginator
return render_to_response('meddy1/doclistings.html',d)
答案 0 :(得分:2)
使用order_by():
doctors = Doctor.objects.all().order_by('-likes')
您还可以在Model Meta类中定义default ordering:
class Doctor(models.Model):
class Meta:
ordering = ['-likes']