Django/python images not displayed

I have tried all combinations (ex: {{ cars.image_main.url }} , {{ car.image_main.url }}).

If anyone can give me a hand I will appreciate it. I'm a beginner at this, I don't have enough time to learn and practice and I follow sometimes youtube and udemy classes. I'm on this for 1 day and my brain is not working at all as far as I can see.

If i will delete all HTML from Template and write {{ cars.image_main.url }} the image will show up. When i put the HTML template back and insert the {{ cars.image_main.url }} in to the code... the image do now show up.

Thank you in advance for your advice or solution.

models.py:

class Car(models.Model):
    dealer = models.ForeignKey(Dealer, on_delete=models.DO_NOTHING)
    brand = models.CharField(max_length=100)
    CATEGORY = (
        ('New', 'New'),
        ('Used', 'Used')
    )
    category = models.CharField(max_length=50, choices=CATEGORY)
    image_main = models.ImageField(upload_to='images')
    image1 = models.ImageField(upload_to='images', blank=True)
    image2 = models.ImageField(upload_to='images', blank=True)
    image3 = models.ImageField(upload_to='images', blank=True)
    image4 = models.ImageField(upload_to='images', blank=True)
    image5 = models.ImageField(upload_to='images', blank=True)
    image6 = models.ImageField(upload_to='images', blank=True)
    image7 = models.ImageField(upload_to='images', blank=True)
    image8 = models.ImageField(upload_to='images', blank=True)
    image9 = models.ImageField(upload_to='images', blank=True)
    image10 = models.ImageField(upload_to='images', blank=True)
    image11 = models.ImageField(upload_to='images', blank=True)
    image12 = models.ImageField(upload_to='images', blank=True)
    image13 = models.ImageField(upload_to='images', blank=True)
    image14 = models.ImageField(upload_to='images', blank=True)
    image15 = models.ImageField(upload_to='images', blank=True)
    body_style = models.CharField(max_length=100, blank=True)
    engine = models.CharField(max_length=100, blank=True)
    stock_number = models.IntegerField(blank=True, null=True)
    mpg = models.CharField(max_length=100, blank=True)
    exterior_color = models.CharField(max_length=100, blank=True)
    interior_color = models.CharField(max_length=100, blank=True)
    drivetrain = models.CharField(max_length=100, blank=True)
    mileage = models.IntegerField(blank=True, null=True)
    sold = models.BooleanField(default=False, blank=False)
    
    transmission = models.CharField(max_length=50, blank=True)
    YEAR_CHOICES = [(r, r) for r in range(2005, datetime.date.today().year+1)]
    year = models.IntegerField(
        ('year'), choices=YEAR_CHOICES, default=datetime.datetime.now().year)
    power = models.IntegerField()
    fuel = models.CharField(max_length=50, blank=True)
    price = models.IntegerField()
    description = models.TextField()
    date = models.DateField()
    
    def __str__(self):
        return self.brand
    
    def get_absolute_url(self):
        return reverse('car_detail', kwargs={
            'car_id': self.id
        })

views.py:

def car_detail(request, car_id):
    cars = get_object_or_404(Car, id=car_id)

    context = {
        'cars': cars
    }
    return render(request, 'car_detail.html', context)

car_detail.html:

    <li data-thumb="images/thumbnail1.jpg"> <a href="#"><img src="images/thumbnail1.jpg" alt="" /></a> </li>

1 answer

  • answered 2021-09-27 16:31 djangodeveloper

    Try this: car_detail.html:

    <li data-thumb="images/thumbnail1.jpg"> <a href="#"><img src="{{ cars.image_main.url }}" alt="" /></a> </li>
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum