title: How do I serve media files in a local Django environment?
body: <p>I can upload an image through the admin page, but the image can not be found when I navigate to the url that is generated by django. (404 error)
  The files are being uploaded to the folder:</p>
  
  <pre><code>project_root/media/eventbanner/1/
  </code></pre>
  
  <p>I have tried multiple solutions but none seem to work for my situation.
  Django 1.10 is being run local on Ubuntu 16.04.</p>
  
  <p>The url I get is:</p>
  
  <pre><code>http://localhost:8000/media/eventbanner/1/banner_image.jpg
  </code></pre>
  
  <p>Media root folder is located at:</p>
  
  <pre><code>/home/username/xxx/xxx/project_name/media
  </code></pre>
  
  <p>Code in HTML file:</p>
  
  <pre><code>&lt;div class="banner-image"&gt;
    &lt;img src="{{ event.eventbanner.banner_image.url }}"/&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>url.py code:</p>
  
  <pre><code>from django.conf.urls import url, include
  from django.contrib import admin
  from . import views
  from django.conf import settings
  from django.conf.urls.static import static
  
  app_name = 'events'
  
  urlpatterns = [
  url(r'^$', views.index, name='index'),
  url(r'^details/(?P&lt;event_id&gt;[0-9]+)/$', views.details, name='details'),
  url(r'^details/(?P&lt;event_id&gt;[0-9]+)/addcomment/$', views.add_comment, name='add_comment'),
  ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  </code></pre>
  
  <p>settings.py</p>
  
  <pre><code>STATIC_URL = '/static/'
  STATICFILES_DIRS =[os.path.join(BASE_DIR, 'static'),]
  
  MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
  MEDIA_URL = '/media/'
  </code></pre>
  
  <p>models.py</p>
  
  <pre><code>def validate_only_one_instance(obj):
      model = obj.__class__
      if (model.objects.count() &gt; 0 and obj.id != model.objects.get().id):
          raise ValidationError("Can only create 1 %s instance" % model.__name__)
  
  class EventBanner(models.Model):
      event = models.OneToOneField(Event, unique=True)
      banner_image = models.ImageField(upload_to=get_image_path, blank=True, null=True)
  
      def clean(self):
          validate_only_one_instance(self)
  </code></pre>