开发者

Can't seem to load images in Django?

开发者 https://www.devze.com 2023-02-20 16:30 出处:网络
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.

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/'

0

精彩评论

暂无评论...
验证码 换一张
取 消