Home > Software design >  Why is Django trying to find my image in such directory?
Why is Django trying to find my image in such directory?

Time:11-19

Instead"/media/", it tries to find here ???

The idea was to put several images in one object and everything works in the admin panel, but in the html template it paves the wrong path to the image. Tell me what am I doing wrong?

models.py `

class Product(models.Model):
    name = models.CharField(max_length=255, verbose_name='Название товара')
    description = models.TextField(blank=True, verbose_name='Описание')
    price = models.DecimalField(max_digits=10, decimal_places=0, verbose_name='Цена')
    created = models.DateTimeField(auto_now_add=True, verbose_name='Время создания')
    updated = models.DateTimeField(auto_now=True, verbose_name='Время обновления')
    is_published = models.BooleanField(default=True, verbose_name='Публикация')
    available = models.BooleanField(default=True, verbose_name='Наличие')
    catalog = models.ForeignKey('Catalog', on_delete=models.PROTECT, verbose_name='Каталог')


    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('product', kwargs={'product_id': self.pk})

    class Meta:
        verbose_name = "Товар"
        verbose_name_plural = "Товары"
        ordering = ['created']


class Images(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='images')
    images = models.ImageField(upload_to='images/%Y/%m/%d/')

    def __str__(self):
        return self.product.name

`

admin.py

`

class ImagesInline(admin.TabularInline):
    fk_name = 'product'
    model = Images


@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    inlines = [ImagesInline, ]
    list_display = ('id', 'name', 'price', 'created', 'updated', 'is_published', 'available', 'catalog')
    list_display_links = ('id', )
    search_fields = ('name', )
    list_editable = ('name', 'price', 'is_published', 'available', 'catalog')
    list_filter = ('is_published', 'available', 'created', 'catalog')

`

settings.py

`

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

`

shop/urls.py

`

urlpatterns = [
    path('', index, name='home'),
    path('about/', about, name='about'),
    path('catalog/', catalog, name='catalog'),
    path('basket/', cart, name='cart'),
    path('register/', register, name='register'),
    path('delivery/', delivery, name='delivery'),
    path('product/<int:product_id>', show_product, name='product'),
    path('category/<int:catalog_id>', show_category, name='category')[enter image description here](https://i.stack.imgur.com/xf9r5.png)
]

`

The only way it works for me is to just add several fields to the Product model.

image_one = image_two = image_three =

But I still want to fix my mistake. I really hope for your help!

CodePudding user response:

Add this to your project urls.py (not the one in the app).

urlpatterns  = static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns  = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  • Related