Django 似乎无法加载图像?

发布于 2024-10-27 18:50:23 字数 1530 浏览 1 评论 0原文

这应该是非常简单的,但我有一种感觉,其中一个路径名把整个事情搞砸了。基本上,我的图像显示为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无妨# 2024-11-03 18:50:23

您的 ADMIN_MEDIA_PREFIX 可能也是 /media/。改变其中之一。

Your ADMIN_MEDIA_PREFIX is probably also /media/. Change one of them.

毁虫ゝ 2024-11-03 18:50:23

更改 MEDIA_URL

MEDIA_URL = 'http://127.0.0.1:8000/media/'

change the MEDIA_URL

MEDIA_URL = 'http://127.0.0.1:8000/media/'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文