Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show images in Django templates

Can someone help me with this issue: I have a Django porject,

in settings.py

 MEDIA_ROOT = 'C:/Users/hl/workspace/beer/media'
 MEDIA_URL = '/media/'
 STATICFILES_DIRS = (
    'C:/Users/hl/workspace/beer/media'
 )

and in models.py

image1= models.ImageField(upload_to=settings.MEDIA_ROOT)

and in url.py

 (r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT}),

in views

def allBeer(request): 
      beers=Beer.objects.all().order_by("name")
      context={'beers': beers}
      return render_to_response('AllBeers.html',context,context_instance=RequestContext(request))

and in html

 {%for beer in beers %}
    <p>
        <a href="/beers/{{beer.slug}}/">
            <img scr="{{beer.image1.url}}">{{beer}}
        </a>
    </p>
 {% endfor%}

It has not problem to load images, but images wont show in html file. I have searched and read a lot from internet but I still couldn't figure out.

Can anyone tell me why?

like image 768
hln Avatar asked Mar 02 '13 13:03

hln


People also ask

How do I view images in Django?

How you specify the location of an image in Django is in between {% %}. In between these brackets, you specify static 'images\\Python. png', where Python is the image you want to display which is inside of the images directory in the static directory you create for the current app you are in.

How do I display an image in HTML template?

To achieve this, you will have to create mime object for image and populate the contents of this mime with data of the image. Then in your HTML you will have to give the id of this mime object as img src. Effectively, you are embedding the image.


2 Answers

image1= models.ImageField(upload_to=images)


from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from project_name import settings

admin.autodiscover()
urlpatterns = patterns('',
    ...........
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += staticfiles_urlpatterns()


<img src="{{MEDIA_URL}}{{beer.image1}}">

settings.py

import os

PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..')
SITE_ROOT = PROJECT_ROOT


MEDIA_ROOT = os.path.join(SITE_ROOT, 'media')
MEDIA_URL = '/media/'


STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
STATIC_URL = '/static/'


STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    os.path.join(SITE_ROOT, 'staticfiles'),
)

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(SITE_ROOT, 'templates'),
)
like image 172
catherine Avatar answered Sep 22 '22 08:09

catherine


You're messing with the src attribute of the image. It should just be -

<img src="{{beer.image1.url}}" /> <!-- from the media url -->

Don't add anything to the end - django know's the url to serve the image from - that's what the ImageField on the model does.

I don't know that there's actually anything wrong with your url conf, but the pattern recommended in the docs is -

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = patterns('',
    # ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
like image 44
Aidan Ewen Avatar answered Sep 20 '22 08:09

Aidan Ewen