Home > Blockchain >  MY 'image' attribute has no file associated with it
MY 'image' attribute has no file associated with it

Time:02-12

With my reading of many answers to questions similar to mine, all of those answers did not find a solution to my problem.. so I will present them so that I may find a solution that fits my problem: my View file: enter image description here

my Template File: enter image description here

my Admin file: enter image description here

The Error: enter image description here enter image description here

I tried every solution with no hope.

from datetime import datetime

from ckeditor.fields import RichTextField from multiselectfield import MultiSelectField from django.db import models

class Car(models.Model): state_choice = ( ('AL', 'Alabama'), ('AK', 'Alaska'), ('AZ', 'Arizona'), ('AR', 'Arkansas'), ('CA', 'California'), ('CO', 'Colorado'), ('CT', 'Connecticut'), ('DE', 'Delaware'), ('DC', 'District Of Columbia'), ('FL', 'Florida'), ('GA', 'Georgia'), ('HI', 'Hawaii'), ('ID', 'Idaho'), ('IL', 'Illinois'), ('IN', 'Indiana'), ('IA', 'Iowa'), ('KS', 'Kansas'), ('KY', 'Kentucky'), ('LA', 'Louisiana'), ('ME', 'Maine'), ('MD', 'Maryland'), ('MA', 'Massachusetts'), ('MI', 'Michigan'), ('MN', 'Minnesota'), ('MS', 'Mississippi'), ('MO', 'Missouri'), ('MT', 'Montana'), ('NE', 'Nebraska'), ('NV', 'Nevada'), ('NH', 'New Hampshire'), ('NJ', 'New Jersey'), ('NM', 'New Mexico'), ('NY', 'New York'), ('NC', 'North Carolina'), ('ND', 'North Dakota'), ('OH', 'Ohio'), ('OK', 'Oklahoma'), ('OR', 'Oregon'), ('PA', 'Pennsylvania'), ('RI', 'Rhode Island'), ('SC', 'South Carolina'), ('SD', 'South Dakota'), ('TN', 'Tennessee'), ('TX', 'Texas'), ('UT', 'Utah'), ('VT', 'Vermont'), ('VA', 'Virginia'), ('WA', 'Washington'), ('WV', 'West Virginia'), ('WI', 'Wisconsin'), ('WY', 'Wyoming'), )

features_choices = (
    ('Cruise Control', 'Cruise Control'),
    ('Audio Interface', 'Audio Interface'),
    ('Airbags', 'Airbags'),
    ('Air Conditioning', 'Air Conditioning'),
    ('Seat Heating', 'Seat Heating'),
    ('Alarm System', 'Alarm System'),
    ('ParkAssist', 'ParkAssist'),
    ('Power Steering', 'Power Steering'),
    ('Reversing Camera', 'Reversing Camera'),
    ('Direct Fuel Injection', 'Direct Fuel Injection'),
    ('Auto Start/Stop', 'Auto Start/Stop'),
    ('Wind Deflector', 'Wind Deflector'),
    ('Bluetooth Handset', 'Bluetooth Handset'),
)

door_choices = (
    ('2', '2'),
    ('3', '3'),
    ('4', '4'),
    ('5', '5'),
    ('6', '6'),
)

transmission_choice = (
    ('A', 'Auto_shift'),
    ('M', 'Manual_shift'),
)

fuel_type_choices = (
    ('P', 'Petrol'),
    ('D', 'Diesel'),
    ('CNG', 'Compressed Natural Gas'),
    ('Et', 'Ethanol'),
    ('E', 'Electricity'),
)

year_choice = []
for r in range(2000, (datetime.now().year 1)):
    year_choice.append((r, r))

car_title = models.CharField(max_length=255)
city = models.CharField(max_length=100)
state = models.CharField(choices=state_choice, max_length=100)
color = models.CharField(max_length=100)
model = models.CharField(max_length=100)
year = models.IntegerField(('year'), choices=year_choice)
condition = models.CharField(max_length=100)
price = models.IntegerField()
description = RichTextField()
car_photo = models.ImageField(upload_to='photo/%Y%m%d/', blank=True)
car_photo_1 = models.ImageField(upload_to='photo/%Y%m%d/', blank=True)
car_photo_2 = models.ImageField(upload_to='photo/%Y%m%d/', blank=True)
car_photo_3 = models.ImageField(upload_to='photo/%Y%m%d/', blank=True)
car_photo_4 = models.ImageField(upload_to='photo/%Y%m%d/', blank=True)
features = MultiSelectField(choices=features_choices)
body_style = models.CharField(max_length=100)
engine = models.CharField(max_length=100)
transmission = models.CharField(
    choices=transmission_choice, max_length=100)
interior = models.CharField(max_length=100)
miles = models.IntegerField()
doors = models.CharField(choices=door_choices, max_length=10)
passengers = models.IntegerField()
vin_no = models.CharField(max_length=100)
milage = models.IntegerField()
fuel_type = models.CharField(choices=fuel_type_choices, max_length=50)
no_of_owners = models.IntegerField()
is_featured = models.BooleanField(default=False)
created_date = models.DateTimeField(default=datetime.now, blank=True)

after trying the proposed solution: enter image description here

CodePudding user response:

It seems like it's just car 4 that has no image associated with it. Look at the admin. Car 4 exists, but it has no image associated with it, and your template is not checking for that.

{% if car.car_photo_4 %}   <!-- just checks if car_photo_4 exists, which it does -->

So try changing it to:

{% if car.car_photo_4.url %}   

Note that this is probably also not correct, but I'd need to see your Car model to make it better.

CodePudding user response:

Your issue here is that by checking for {% if car.car_photo_4 %}, you're effectively checking if the attribute exists for this model (which it does, it's just empty). Since we know that car_photo_4 is always defined for the Car model, you should be checking that the url attribute is not empty or null.

You can do this by:

{% if car.car_photo_4.url %}

As a side note, you should use a ManyToManyField instead of statically defining the images as attributes to the Car model as it makes it simply to add more images for the Car model.

  • Related