有哪些简单的选项可以为用户提供排序结果的可能性?

时间:2015-07-23 18:09:53

标签: django sorting django-templates django-views

假设我正在创建房地产代理网页。优惠页面最初显示按pub_date订购的优惠。我希望用户能够按pricefloor_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 %}

0 个答案:

没有答案