I am new to django and I played with downloading photos and then displaying them .... well, trying to display them.
Whenever I try to display an image from a template, I just get an icon with a broken image.
I am using sqlite3 server
settings.py
ROOT_DIR = os.path.dirname(os.path.dirname(__file__)) def location(f): return os.path.join(ROOT_DIR, f) MEDIA_URL = 'http://127.0.0.1:8000/media/' MEDIA_ROOT = location('media/')
models.py
class Image(models.Model): image = models.ImageField(upload_to = 'images/')
views.py
from imageupload.settings import MEDIA_ROOT, MEDIA_URL def main(request): imgs = Image.objects.all() return render_to_response('index.html', {'images': imgs, 'media_root': MEDIA_ROOT, 'media_url': MEDIA_URL})
url.py
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Now I just used admin to upload images. And that seems to work fine, they go where I expect
But when I try to display them:
template
<!DOCTYPE html> <html> <img src="<correct path to project>/media/images/photo_1.JPG" /> {% for img in images %} <img src="{{ media_root }}{{ img.image.name }}" /> <img src="{{ media_url }}{{ img.image.name }}" /> <img src="{{ img.image.url }}" /> {% endfor %} </html>
I get a broken icon for everyone.
The source code of the browser shows me:
<!DOCTYPE html> <html> <img src="<correct path to project>/media/images/photo_1.JPG" /> <img src="<correct path to project>/media/images/photo_1.JPG" /> <img src="http://127.0.0.1:8000/media/images/photo_1.JPG" /> <img src="http://127.0.0.1:8000/media/images/photo_1.JPG" /> </html>
which makes sense, I only have one photo uploaded.
and if I copy one of the hard links and put it in some other html file ... it works