Python 德扬戈。产品的几个图像

Python 德扬戈。产品的几个图像,python,django,Python,Django,我想为我的产品添加几个图像,并且已经有了 models.py: class Product(models.Model): category = models.ForeignKey(Category, related_name='products') name = models.CharField(max_length=200, db_index=True) slug = models.SlugField(max_length=200, db_index=True)

我想为我的产品添加几个图像,并且已经有了

models.py:

class Product(models.Model):
    category = models.ForeignKey(Category, related_name='products')
    name = models.CharField(max_length=200, db_index=True)
    slug = models.SlugField(max_length=200, db_index=True)
    short_description = models.CharField(max_length=200, blank=True)
    description = models.TextField(blank=True)
    product_information = models.TextField(blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=0)
    stock = models.PositiveIntegerField()
    available = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    class Meta:
        ordering = ('name',)
        index_together = (('id', 'slug'),)
    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('shop:product_detail',
                        args=[self.category.slug, self.slug])

class Images (models.Model):
    product = models.ForeignKey(Product, default=None, related_name='images')
    image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
但我不能通过管理员页面上传图片。 管理员

class ProductAdmin(admin.ModelAdmin):
    list_display = ['name', 'slug', 'category', 'price', 'stock', 'available', 'created', 'updated']
    list_filter = ['available', 'created', 'updated', 'category']
    list_editable = ['price', 'stock', 'available']
    prepopulated_fields = {'slug': ('name',)}
admin.site.register(Product, ProductAdmin)
我应该如何更改代码,以便能够使用管理页面为我的产品添加多个图像? 我应该如何在页面代码中调用图像

现在我使用{{product.image}调用image


我已经花了很多时间来寻找答案。请帮忙

这里可以使用的是管理内联线,帮助您轻松编辑/更新相关项目

class ProductImagesInline(admin.StackedInline):
    model = Images

class ProductAdmin(admin.ModelAdmin):
    list_display = ['name', 'slug', 'category', 'price', 'stock', 'available', 'created', 'updated']
    list_filter = ['available', 'created', 'updated', 'category']
    list_editable = ['price', 'stock', 'available']
    prepopulated_fields = {'slug': ('name',)}
    inlines = [ProductImagesInline]
一旦有了图像,您就可以像
product.images.all()

一样访问它们。请为
图像使用一个管理员(是的,您应该给它一个单数名称!)型号:

<>在模板中适当显示图像时,应该考虑缩略图库,如:
#一些示例
{%用于product.images.all%中的图像}
{%thumbnail image.image“500x500”format=“PNG”upscale=False as thumb%}
#^^^^^此处需要图像字段
{%endthumbnail%}
{%endfor%}

除此之外,您还可以通过访问
图像的url。这是您的原始图像文件将被送达的地方。这要求您正确配置
MEDIA\u ROOT
MEDIA\u URL

django返回名称错误:名称“Image”未定义是的,我擅自更改了您的型号名称,从
“Images”
;-)django返回NameError:name'Images'未定义您需要从顶部的模型导入它
models.py

class Product(models.Model):
    ''''fields''''

class Image(models.Model):
    image = models.ImageField()
    product = models.ForeignKey(Product, default=None, related_name='images',on_delete=models.PROTECT)

admin.py

from yourapp.models import Product, Images

class InlineImage(admin.TabularInline):
    model = Images

class ProductAdmin(admin.ModelAdmin):
    inlines = [InlineImage]

admin.site.register(Product, ProductAdmin)
# sorl example
{% for image in product.images.all %}
    {% thumbnail image.image "500x500" format="PNG" upscale=False as thumb %}
    #                  ^^^^^ you need the image field here
    <img src="{{ thumb.url }}" alt="{{ product.name }}">
    {% endthumbnail %}
{% endfor %}
models.py

class Product(models.Model):
    ''''fields''''

class Image(models.Model):
    image = models.ImageField()
    product = models.ForeignKey(Product, default=None, related_name='images',on_delete=models.PROTECT)

admin.py

from yourapp.models import Product, Images

class InlineImage(admin.TabularInline):
    model = Images

class ProductAdmin(admin.ModelAdmin):
    inlines = [InlineImage]

admin.site.register(Product, ProductAdmin)