Django Template Check if there is image

I’m trying to display a banner if the user has uploaded a banner for that item.
This is the code I’m using, but it’s calling True every time, even when the model doesn’t have an image uploaded to it.

class category(models.Model):
image = models.ImageField(blank=True, null=True)

I’m using the template

{% if category.image %}

{% endif %}

Hello there!
I think that checking for a None value is the way.

# Checking for having a image
{% if category.image is not None %}
{% endif %}

# Checking for non-existence of a image
{% if category.image is None %}
{% endif %}

tried it sadly it doesn’t work

We’ll need to see the view to see what you’re passing to the template in the context. We may also need to see more of the template.

Note: When posting code and templates here, surround each file between lines of three backtick - ` characters. This means you’ll have a line of ```, then the code (or template) then another line of ```.

I’m getting an error where I have the image for one category but not the others, but it still shows the placeholder image for the categories that don’t have images.

I thought it would be pretty straightforward, but I’m definitely missing something.
Hopefully someone here has the answer.

![Screen Shot 2023-04-18 at 7.43.10 PM|242x500]

(upload://4LlwYR8iw0MdCe4WNbvS9CnKSkP.png)
.views

def order(request, restaurant_urlname, table_number):
  categories = Category.objects.filter(restaurant = restaurant)
  context = {'categories':categories,}
  return render(request, 'order.html', context)

.model

class Category(models.Model):
  image = models.ImageField(upload_to='category_images', blank=True, null=True)

.html

{% for category in categories %}
          {% if category.image %}
            <img src="{{category.image.url}}" style="width:100%; height:auto;">
          {% endif %}
{% endfor %}      

I’m guessing this isn’t the complete view - otherwise, the view itself would throw an error and not run. (You’re passing a parameter into the view named restaurant_urlname, but your query is looking for a variable named restaurant.)
Please don’t edit or retype information requested here - it really does work best if you just copy/paste the code.

Side note - you might try {% if category.image.url %}

Gotcha,

.model

class Category(models.Model):
  user = models.ForeignKey(User, on_delete=models.CASCADE)
  name = models.CharField(max_length=64)
  restaurant = models.ForeignKey(Restaurant, related_name="categories", on_delete=models.CASCADE)
  image = models.ImageField(upload_to='category_images', blank=True, null=True)
  class Meta:
    verbose_name_plural = 'Categories'

.html

 {% for category in categories %}
          {% if category.image.url %}
            <img src="{{category.image.url}}" style="width:100%; height:auto;">
          {% endif %}
{% endfor %}

.views

def order(request, restaurant_urlname, table_number):
  restaurant = Restaurant.objects.get(urlname=restaurant_urlname)
  table = Table.objects.get(restaurant=restaurant, number=table_number)
  categories = Category.objects.filter(restaurant = restaurant)
  #Check if there is a request.session, if not create one.
  request.session.set_expiry(604800) 
  try:
    session_id = request.COOKIES['sessionid']
  except:
    request.COOKIES['sessionid'] = uuid.uuid4
    session_id = request.COOKIES['sessionid']
  
  order, created = Order.objects.get_or_create(session_id = session_id, paid=False)
  order.restaurant = restaurant
  order.table = table
  order.save()
  
  context = {
    'restaurant':restaurant,
    'categories':categories,
    'table': table,
    'order':order,
  }
  
  return render(request, 'order.html', context)

I’m assuming that what you’re seeing is the placeholder icon on the page.

What does the html in the browser look like for one of these entries?
What does the http request in the console log look like?

Lol i’m retarded, i think it just created a bunch of images named 1 when i created the model field.

It worked for me simply by using:
{% if category.image %}
{% endif %}

1 Like