Can't seem to load images in Django?
This ought to be quite straight forward but I have a feeling one of the pathnames is screwing the whole thing up. Basically, my images are displaying as 404.
Here are the relevant parts to my Django app:
models.py
class MemeDetails(models.Model):
datecreated = models.DateTimeField(default=datetime.datetime.now)
profileimage = models.ImageField(upload_to="memeimages", blank=True)
name = models.CharField(max_length=50)
description = models.CharField(max_length=1000)
origin = models.CharField(max_length=50)
origindate = models.DateField()
settings.py
MEDIA_ROOT = '/home/nai/Projects/meme/media/'
MEDIA_URL = '/media/'
So all the images go into this folder home/nai/Projects/meme/media/memeimages
urls.py
urlpatterns = patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
views.py
def memepage(request, memeid):
q = MemeDetails.objects.get(id=memeid)
memeimage = q.profileimage
return render_to_response('memepage.html', {'memeid': memeid, 'memeimage': memeimage},context_instance=RequestContext(req开发者_StackOverflow中文版uest))
template
<img src="{{ MEDIA_URL }}{{memeimage }}" />, {{ memeid }}
The actual URL of the image is at this URL http://127.0.0.1:8000/media/memeimages/flickrphotobrowser.png
but I am getting a 404.
P.S I know I should use Apache to serve my static images on my production environment. I'm just trying to get this to work.
Help!
Your ADMIN_MEDIA_PREFIX
is probably also /media/
. Change one of them.
change the MEDIA_URL
MEDIA_URL = 'http://127.0.0.1:8000/media/'
精彩评论