title: How to use if statement inside a block in Django for image retrieval
body: <p>I'm stuck.  I have been trying to use all of my google fu to find this answer, but can't find it asked or answered anywhere.  This is one of my first projects that I am trying to do not following a tutorial, so I apologize for my ignorance ahead of time.  I am building my first blog and am trying to add a feature to display an image in the article.  The problem is, I only want it to display if there is an image, as not all of them will have images.  I can get the image to display, but if I go to an article without one, it gives me an error saying that it can't find the image.  I thought I would try to do an if statement, but I am getting an error there as well and am not sure if I am doing the statement incorrectly (which is my guess) or not.  </p>
  
  <pre><code>#models.py
  from django.db import models
  from datetime import datetime
  
  # Create your models here.
  
  class Post(models.Model):
      title = models.CharField(max_length=200)
      body = models.TextField()
      created_at = models.DateTimeField(default=datetime.now, blank=True)
      image      = models.ImageField(upload_to='blog/', null=True, blank=True)
  
      def __str__(self):
          return self.title
  
  
  
  
  #view.py
  from django.shortcuts import render
  from django.http import HttpResponse
  from .models import Post
  
  # Create your views here.
  def index(request):
      posts = Post.objects.all()[:10]
  
      context = {
          'title' : 'Latest Posts',
          'posts' : posts,
      }
  
      return render(request, 'blog/index.html', context)
  
  def details(request, id):
      post = Post.objects.get(id=id)
      queryset = Post.objects.all()
  
      context = {
          'post' : post,
          'object' : queryset
          }
  
      return render(request, 'blog/details.html', context)
  
  
  
  
  #details.html
  {% extends 'blog/layout.html'%}
  
  {% block content %}
  &lt;h3 class="center-align red darken-3"&gt;{{post.title}}&lt;/h3&gt;
  &lt;div class="card hoverable"&gt;
      &lt;div class="card-content teal accent-4"&gt;
          {{post.body}}
      &lt;/div&gt;
      &lt;div class="card-action teal accent-4"&gt;
          {{post.created_at}}
      &lt;/div&gt;
  
      {% if post.image.url %}
  
      {% for post_image in post.image.url %}
  
      &lt;div class="div"&gt;
          &lt;img src='{{post.image.url}}' class='responsive-img' /&gt;&lt;br/&gt;  
      &lt;/div&gt;
      {% else %}
      &lt;div class="div"&gt;
          &lt;/div&gt;
  
      {% endfor %}
      {% endif %}
  &lt;/div&gt;
  &lt;a href="/blog/" class="btn"&gt;Go Back&lt;/a&gt;
  
  {% endblock %}
  </code></pre>