Django 似乎无法加载图像?
这应该是非常简单的,但我有一种感觉,其中一个路径名把整个事情搞砸了。基本上,我的图像显示为 404。
以下是我的 Django 应用程序的相关部分:
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/'
因此,所有图像都会进入此文件夹 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(request))
template
<img src="{{ MEDIA_URL }}{{memeimage }}" />, {{ memeid }}
图片的实际 URL 是在此 URL http://127.0.0.1:8000/media/memeimages/flickrphotobrowser.png
但我得到了 404。
PS 我知道我应该使用 Apache 在我的生产环境中提供静态图像。我只是想让这个发挥作用。
帮助!
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>.*)
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(request))
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!
, 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT}),
)
views.py
template
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
ADMIN_MEDIA_PREFIX
可能也是/media/
。改变其中之一。Your
ADMIN_MEDIA_PREFIX
is probably also/media/
. Change one of them.更改 MEDIA_URL
MEDIA_URL = 'http://127.0.0.1:8000/media/'
change the MEDIA_URL
MEDIA_URL = 'http://127.0.0.1:8000/media/'