gpt4 book ai didi

django - 集成 Photologue

转载 作者:行者123 更新时间:2023-12-04 22:17:14 26 4
gpt4 key购买 nike

我想将 photologue 与我的 Django 应用程序集成,并使用它在车辆库存中显示照片......有点像 Boost Motor Group Inc. 提供的内容。我已经集成了该应用程序,所以下一步我正在尝试弄清楚如何将其连接到我的车辆模型以及如何显示照片。我的车辆模型看起来像这样 BTW

class Vehicle(models.Model):
stock_number = models.CharField(max_length=6, blank=False)
vin = models.CharField(max_length=17, blank=False)
common_vehicle = models.ForeignKey(CommonVehicle)
exterior_colour = models.ForeignKey(ExteriorColour)
interior_colour = models.ForeignKey(InteriorColour)
interior_type = models.ForeignKey(InteriorType)
odometer_unit = models.ForeignKey(OdometerUnit)
status = models.ForeignKey(Status)
odometer_reading = models.PositiveIntegerField()
selling_price = models.PositiveIntegerField()
purchase_date = models.DateField()
sales_description = models.CharField(max_length=60, blank=False)
feature_sets = models.ManyToManyField(FeatureSet, blank=True)
features = models.ManyToManyField(Feature, blank=True)

def __unicode__(self):
return self.stock_number

最佳答案

我使用 ImageKit(太棒了!)

模型.py

from imagekit.models import ImageModel
class Photo(ImageModel):
name = models.CharField(max_length=100)
original_image = models.ImageField(upload_to='photos')
num_views = models.PositiveIntegerField(editable=False, default=0)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')

class IKOptions:
# This inner class is where we define the ImageKit options for the model
spec_module = 'cms.specs'
cache_dir = 'photos'
image_field = 'original_image'
save_count_as = 'num_views'

class Vehicle(models.Model):
images = generic.GenericRelation('Photo', blank = True, null = True)

规范文件
from imagekit.specs import ImageSpec 
from imagekit import processors
from imagekit.lib import *

# first we define our thumbnail resize processor
class ResizeThumb(processors.Resize):
width = 100
height = 75
crop = True

# now lets create an adjustment processor to enhance the image at small sizes
class EnchanceThumb(processors.Adjustment):
contrast = 1.2
sharpness = 1.1

# now we can define our thumbnail spec
class Thumbnail(ImageSpec):
processors = [ResizeThumb, EnchanceThumb]

在您的模板中,您将像这样访问此缩略图:
{% for p in vehicle.images.all %}
{{ p.get_thumbnail.url }}
{% endfor %}

admin.py 可能如下所示:
class ImagesInline(generic.GenericTabularInline):
model = Photo
max_num =4

class VehicleAdmin(admin.ModelAdmin):
inlines = [ImagesInline]

关于 ImageKit

将文件 specs.py 添加到您的应用程序并像这样告诉 ImageKit
  class IKOptions:
# This inner class is where we define the ImageKit options for the model
spec_module = 'cms.specs # ur_app.specs

向您的照片模型添加一个字段以保存它显示的 View /内容类型。即选择字段

在 View /模板中,您可以对其进行过滤

关于django - 集成 Photologue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1556279/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com