【问题标题】:How do I rearrange the cards in ascending order django如何按升序重新排列卡片 django
【发布时间】:2021-11-19 09:39:08
【问题描述】:

我有一个页面,其中卡片中的客户名称未按字母顺序按升序重新排列 例如:B、M、S。我如何让它按字母顺序排列,以便首先出现客户名称为曼谷航空公司的卡片,然后出现马来西亚航空公司的卡片,依此类推?

views.py

def outgoinggallery(request):
    user = request.user

    category = request.GET.get('category')

    if category == None:
        alloutgoinglru = OutgoingLRU.objects.filter(category__user=user)
    else:
        alloutgoinglru = OutgoingLRU.objects.filter(category__name=category, category__user=user)
        # if query:
        # return OutgoingLRU.objects.filter(title__icontains=query)



    categories = Category.objects.filter(user=user)
    context = {'categories': categories, 'alloutgoinglru': alloutgoinglru}

    return render(request, 'Outgoing/outgoinggallery.html', context)

outgoinggallery.html

{% extends "logisticbase.html" %}
{% block content %}
    <!-- CSS only -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">

    <style>



td, th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
  border-radius: 15px;
}
        .image-thumbail {
            height: 200px;
            object-fit: cover;
        }


        .list-group-item a {
            text-decoration: none;
            color: black;
        }
    </style>




    <br>


   <div style="padding-left:16px">

        <div class="row">


            <div class="col-md-9">
                <div class="row">
                    <h5>View Outgoing LRU</h5>
                    <div class="col-md-7">
                    
                    </div>
                    <br>
                    <div class="col-md-9">
                <div class="row">

                    {% for OutgoingLRU in alloutgoinglru %}
                    <div class="col-md-4">
                        <div class="card my-4">
                            <img class="image-thumbail" src="{{OutgoingLRU.image.url}}" >

                            <div class="card-body">
                                <small>Customer Name: {{OutgoingLRU.category.name}}</small>
                                <br>
                                <small>Delivery Order: {{OutgoingLRU.Deliveryor}}</small>
                            </div>
                            <a href="{% url 'viewlruphoto' OutgoingLRU.id %}"  style="width:265px" class="btn btn-outline-dark btn-sm m-1">View</a>
                            <form action="{% url 'deleteoutgoing' OutgoingLRU.id %}" method="post">
                                {% csrf_token %}
                                <button type="submit" style="width:270px" class="btn btn-sm btn-danger">Delete</button>
                            </form>

                        </div>
                    </div>
                    {% empty %}
                    <h3>No photos...</h3>
                    {% endfor %}








                </div>
            </div>
        </div>
   </div>


{% endblock %}

models.py

class OutgoingLRU(models.Model):
    class Meta:
        verbose_name = 'OutgoingLRU'
        verbose_name_plural = 'OutgoingLRUs'

    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, blank=True)
    image = models.ImageField(null=False, blank=False)
    equipmentimages = models.ImageField(null=False, blank=False)
    boximages = models.ImageField(null=False, blank=False)
    documentimage = models.ImageField(null=False, blank=False)

    datetime = models.DateTimeField()  # datetime is the date and time the form was created
    serialno = models.TextField()  # serialno stand for serial number
    partno = models.TextField()  # partno stand for part number
    Deliveryor = models.TextField()  # deliveryor stand for delivery order
    MCO = models.TextField()
    descriptionequipment = models.TextField()  # A short description about the equipment (*Optional)
    descriptionequipmentbox = models.TextField()  # A short description of the equipment in the box (*optional)
    descriptionbox = models.TextField()  # A short description of the box (*optional)
    document = models.TextField()  # A short description of the delivery note document (*optional)


    def __str__(self):
        return self.descriptionequipment

【问题讨论】:

  • 请向我们展示您的模型。
  • 当然,我的模型是要添加到数据库中的
  • 好的,我已经用 models.py 编辑了我的问题

标签: html django django-views django-templates


【解决方案1】:

升序: alloutgoinglru = alloutgoinglru.order_by('category__name')

降序: alloutgoinglru = alloutgoinglru.order_by('-category__name')

【讨论】:

  • 嗨,我试着把它放在我的views.py下它不起作用
  • @amadousow 我可以检查一下订购的作用吗?因为当我添加订单时它给了我一个错误
  • @Shadowwalker 你在哪里添加了 Category 或 OutgoingLRU 中的排序?
  • 呵呵,我的数据库中没有排序
  • @Shadowwalker 如果您将其添加到模型类别中会怎样,例如 class Meta: ordering = ('name', )?
【解决方案2】:

models.py

class Category(models.Model):
    user = models.ForeignKey(
      User, on_delete=models.SET_NULL, null=True, blank=True)
    name = models.CharField(max_length=100, null=False, blank=False)
   
    class Meta:
       ordering = ('name',)
       verbose_name = 'Category'
       verbose_name_plural = 'Categories'

   def __str__(self):
       return self.name


class OutgoingLRU(models.Model):

    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, blank=True)
    image = models.ImageField(null=False, blank=False)
    equipmentimages = models.ImageField(null=False, blank=False)
    boximages = models.ImageField(null=False, blank=False)
    documentimage = models.ImageField(null=False, blank=False)

    datetime = models.DateTimeField()  # datetime is the date and time the form was created
    serialno = models.TextField()  # serialno stand for serial number
    partno = models.TextField()  # partno stand for part number
    Deliveryor = models.TextField()  # deliveryor stand for delivery order
    MCO = models.TextField()
    descriptionequipment = models.TextField()  # A short description about the equipment (*Optional)
    descriptionequipmentbox = models.TextField()  # A short description of the equipment in the box (*optional)
    descriptionbox = models.TextField()  # A short description of the box (*optional)
    document = models.TextField()  # A short description of the delivery note document (*optional)

    class Meta:
        verbose_name = 'OutgoingLRU'
        verbose_name_plural = 'OutgoingLRUs
        ordering = ('category__name',)

    def __str__(self):
        return self.descriptionequipment

之后:
1) 运行 python manage.py makemigrations
2) 运行 python manage.py migrate

【讨论】:

  • 好的,感谢您的帮助,现在可以使用了 :)
  • 嗨@amadou sow,你介意帮我解决这个问题吗? stackoverflow.com/questions/69370717/…
  • @Shadowwalker 我回答了你的问题。
  • HI @amadou sow,感谢您的帮助,我还有一个问题是,例如,当用户编辑序列号表时,如何使操作完成转到第 2 步?
  • @Shadowwalker 我已经回答了你的问题
猜你喜欢
  • 1970-01-01
  • 2012-07-30
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
  • 2020-01-18
  • 1970-01-01
  • 2016-09-07
  • 1970-01-01
相关资源
最近更新 更多