Reputation: 61
The problem is that I get a broken image type. My code is as follows:
in settings.py I have
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
SETTINGS_DIR = os.path.dirname(__file__)
PROJECT_PATH = os.path.join(SETTINGS_DIR, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)
TEMPLATE_DIRS = [os.path.join(PROJECT_PATH, 'templates')]
MEDIA_ROOT = '/home/username/work_station/project1/media/static/pic'
MEDIA_URL = '/media/'
PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..')
SITE_ROOT = PROJECT_ROOT
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
My models.py
class Image(models.Model):
post = models.ForeignKey(Post)
image = models.ImageField(upload_to='static/pic/', default = None)
description = models.CharField(max_length=100)
def __unicode__(self):
My views
class IndexListView(ListView):
template_name = "dashboard.html"
context_object_name = 'latest_pictures'
def get_queryset(self):
return Image.objects.all()
return str(self.image)
my template: dashboard.html
{% for latest in latest_pictures %}
{% load staticfiles %}
<div><img src="{{ MEDIA_URL }}{{ latest.image }}"></div>
{% endfor %}
Upvotes: 0
Views: 1774
Reputation: 37904
actually, your MEDIA_ROOT
should be
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
and Image files are not staticfiles, they are media files. so..
{% for latest in latest_pictures %}
<div><img src="{{ latest.image.url }}"></div>
{% endfor %}
should be good enough.
and btw, upload_to
is a destination under media
folder. upload_to='static/pic/'
is kind of misleading name..
aaand as Daniel stated, if your server is serving your media files correctly, then this should work.
Upvotes: 2