如何强制画廊中图片的顺序

发布于 2024-08-11 09:07:27 字数 675 浏览 4 评论 0原文

我正在构建一个具有图像库的 django 应用程序,并且客户坚持要求图像按特定顺序显示。我使用管理界面上传图像并编辑其属性,并且我的模型中有一个 ImageFile 类,基本上如下所示:

class ImageFile(models.Model):
    """represents an image file"""

    # the image description
    description = models.CharField(max_length=45)

    # the actual image
    image = models.ImageFile(upload_to='images') 

    # running number representing the order on the page
    order = models.IntegerField()

    def __unicode__(self):
          return "%s" % (self.description)

    class Meta:
        db_table = 'images'

我正在使用 IntegerField ' order' 具有控制排序的流水号。我认为必须有一种更智能/更好的方法来做到这一点(另一种模型?),并且还能够通过管理界面轻松控制它。

I'm building a django app that has an image gallery, and the client insists the images be displayed in specific order. I use the admin interface to upload the images and edit their properties, and I have an ImageFile class in my model that basically looks like this:

class ImageFile(models.Model):
    """represents an image file"""

    # the image description
    description = models.CharField(max_length=45)

    # the actual image
    image = models.ImageFile(upload_to='images') 

    # running number representing the order on the page
    order = models.IntegerField()

    def __unicode__(self):
          return "%s" % (self.description)

    class Meta:
        db_table = 'images'

I'm using the IntegerField 'order' to have running number that'll control the sorting. I figured there has to be a smarter/better way to do this (another model?) and also be able to easily control it through the admin interface.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒儿 2024-08-18 09:07:27

我想您希望为用户提供对图像进行排序的可能性(无论如何,如果您想通过时间添加对其进行排序,最好的方法是按 id 排序),所以,如果有像 Gallery(图像)这样的模型,也许您应该存储元组来自图库的图像 ID(在数据库中作为文本对象)。读完后将其转换为元组,并且您已经得到了预期的顺序。希望我能帮忙。

I suppouse you would like give possibility to sort images to user, (anyway if you want sort it via time add, best way is order it by id), so, if there is model like Gallery (of images) maybe you should store tuple of ids of images from the galery (in DB as a text object). After read cast it to tuple, and you have expected order. Hope I help.

羁拥 2024-08-18 09:07:27

如果图像的顺序是它们上传的顺序,您可以使用时间戳来对它们进行排序。

if the order of the images is the order that they are uploaded you could use a timestamp to order them,.

才能让你更想念 2024-08-18 09:07:27

我使用相同的方法(在模型中使用整数“order”字段)来定义顺序。但是,我自定义了管理员,允许拖放属于相册的图像来定义顺序。当管理员点击“保存”按钮时,拖放后将根据当前订单自动计算订单。所有数据都会提交到服务器保存到DB。

I used the same method (with integer "order" field in the model) to define the ordering. However, I customized the admin to allow drag and drop the images belong to an album to define the order. When the admin hits "save" button, the order will be calculated automatically based on the current order after drag-and-drop. All data will be submitted to the server for saving to DB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文