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)
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 %}
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.
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 %}