如何强制画廊中图片的顺序
我正在构建一个具有图像库的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想您希望为用户提供对图像进行排序的可能性(无论如何,如果您想通过时间添加对其进行排序,最好的方法是按 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.
如果图像的顺序是它们上传的顺序,您可以使用时间戳来对它们进行排序。
if the order of the images is the order that they are uploaded you could use a timestamp to order them,.
我使用相同的方法(在模型中使用整数“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.