假设我正在创建房地产代理网页。优惠页面最初显示按pub_date
订购的优惠。我希望用户能够按price
或floor_space
对商品进行排序。
E.g:
Models.py:
class Offer(models.Model):
city = models.CharField(max_length=128)
province = models.CharField(max_length=128)
nb_rooms = models.IntegerField()
floor_space = models.IntegerField()
price = models.IntegerField()
pub_date = models.DateTimeField(default=timezone.now())
Views.py:
def flats(request):
flat_offers = Offer.objects.order_by('-pub_date')
context_dict = {
'flat_offers': flat_offers,
}
return render(request, 'cardom/flats.html', context_dict)
模板:
{% for offer in flat_offers %}
<table class="table_center">
<tr><th>{{offer.category}}</th></tr>
<tr><td><div class="location">{{offer.city}} ({{offer.district}})</div></td></tr>
<tr><td><img src="{{ offer.main_image.url }}" height="180" width="270" alt="error"></td></tr>
<tr><td><div class="info"><p class="text-left">{{offer.floor_space}}m2</p><p class="text-center">{{offer.get_transaction_display}}</p><p class="text-right">{{offer.price}}zł </p></div></td></tr>
</table><br />
{% endfor %}