The Messie
The Messie

Reputation: 103

Django does not display images

I am trying to create a site with Django, however, I cannot get images to be displayed.

My model is:

class Place(models.Model):
Pic=models.ImageField(upload_to="media",blank=True,null=True)
StoreName=models.CharField(max_length=200)
Address=models.CharField(max_length=200)
OpenDate=models.DateField('date opened')
SubmitDate=models.DateTimeField('date submitted')
def __unicode__(self):
    return self.StoreName  

and the view is:

def PlaceView(request,thisPlace):
PlaceObject=Place.objects.get(StoreName=thisPlace)
return render_to_response('Pages/place.html',{'PlaceObj':PlaceObject})

then in "place.html"

<img src="{{PlaceObj.Pic.url}}" alt="test" width="50px" height="50px"

But the picture is never diplayed. In the console I get a 404 error:

[02/Aug/2012 14:49:50] "GET /Media/media/24228302_1.jpg HTTP/1.1" 404 2220

I have set the MEDIA_ROOT to a folder (/home/Project_root/Media/) in my system and the MEDIA_URL ='http://127.0.0.1:8000/Media/'

Is there something I am not doing right?

Upvotes: 0

Views: 600

Answers (1)

Amit
Amit

Reputation: 991

You have to set a URL pattern in urls.py (or wherever your URL patterns are located) for serving media images. Like this:

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

Make sure to include from django.conf import settings in your urls.py file.

Upvotes: 2

Related Questions