21

我正在本地机器上使用 Django(1.5.1) 构建一个画廊。在我的专辑模型中,我有一个ImageField. 有一个视图可以显示相册的所有图像。它运作良好,但最后图像不显示。如您所见,图像有边框,但无法加载图像。

截屏

在此处输入图像描述

模型.py

class Category(models.Model):
 ###  
class Album(models.Model):   
    category = models.ForeignKey(Category, related_name='albums')
 ###
class Image(models.Model):
    album = models.ForeignKey(Album)
    image = models.ImageField(upload_to = 'images/albums/')

视图.py

def detail(request, album_id):
    album = get_object_or_404(Album, pk=album_id)
    return render(request, 'gallery/detail.html', {'album': album})

详细信息.html

<h1>{{ album.title }}</h1>
{% for image in album.image_set.all %}
  <a>  <img src="{{ image.image.url }}" height="420"></a>
{% endfor %}

如果这是我的相册地址:http://localhost:8000/gallery/1/

然后图片网址是:http://localhost:8000/media/images/albums/photo_4.JPG (I get 404 when enter it in browser)

此媒体根目录和网址:

MEDIA_ROOT = '/media/'    
MEDIA_URL = '/localhost:8000/media/'  

我的媒体根有777 权限

我现在该怎么办?问题出在哪里?

4

7 回答 7

36

我有什么问题的线索。MEDIA_URL应该是这样的:

MEDIA_ROOT='<the full path to your media folder>' (i.e: '/home/ike/project/media/')
MEDIA_URL='/media/'

注意开头的斜线字符。这是因为 media 是您的根服务器文件夹中的一个文件夹,与您调用的任何其他 url 无关。

并将这些行添加到urls.py文件的末尾:

# You might need to import static function like this:
#from django.contrib.staticfiles.urls import static

urlpatterns += staticfiles_urlpatterns()
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

您可以查看以下文档:https ://docs.djangoproject.com/en/dev/howto/static-files

希望这可以帮助

于 2013-04-24T17:54:57.167 回答
5

如果您使用的是开发服务器,那么您需要在 urls.py 中添加一些内容以使 django 为媒体文件提供服务,参见:

1.4.x:https ://docs.djangoproject.com/en/1.4/howto/static-files/#serving-other-directories 1.5.x:https ://docs.djangoproject.com/en/dev/howto/静态文件/#serving-files-uploaded-by-a-user

于 2013-04-24T16:06:57.187 回答
3

检查您定义的settings.pyMEDIA_ROOT和“MEDIA_URL” (它们是正确的)。MEDIA_ROOT 指定计算机上将存储媒体的绝对文件夹。

举个例子:

MEDIA_ROOT = '/myfolder/'

这意味着它将在以下位置查找图像:

/myfolder/images/albums/

接下来在您的settings.py 中检查您的MEDIA_ROOT位置:即

MEDIA_URL = 'http://localhost/myfolder/'

所以你的图片:

<img src="{{ MEDIA_URL }}{{ image.image.url }}" height="420"></a>

这将涉及:

http://localhost/myfolder/images/albums/

希望这可以帮助。

于 2013-04-24T16:01:39.067 回答
2

来源:https ://docs.djangoproject.com/en/dev/howto/static-files/#serving-files-uploaded-by-a-user

 from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

您需要添加到 url 模式以提供上传的文件

于 2015-08-22T01:38:43.087 回答
1

在您的details.html中,更改您的

img src="{{ image.image.url }}" height="420"

img src="your_app/media/{{ image.image.url }}" height="420"

我希望这有帮助。如果不是,我很乐意提供更多细节。

于 2014-01-15T18:57:16.427 回答
1

好吧,我知道这个问题很老,但是在证明了所有选项后我现在就解决了:

  1. 设置.py:
    MEDIA_URL = '/媒体/'
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
  1. urls.py 危险!!!这里我的一个错误是我使用了 my_app/urls.py... 如果你想使用 my_app/urls.py,你需要将“/namespace_name{{ image.image.url }}”添加到 img' src:
    从 django.conf.urls.static 导入静态
    从 django.contrib.staticfiles.urls 导入 staticfiles_urlpatterns
    从 django.conf 导入设置

    urlpatterns += staticfiles_urlpatterns()
    urlpatterns += 静态(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
  1. 你的模板,detail.html
    img src="{{ image.image.url }}" alt="{{ image.title }}"

注意:您不需要 MEDIA_URL,请注意“/”,因为 image.image.url 是绝对的,所以如果您使用命名空间,则不需要添加结束斜杠。

    img src="/namespace_name/{{ image.image.url }}" --> 不好!!!
    img src="/namescape_name{{ image.image.url }}" --> 好!!!
于 2017-07-24T02:48:52.847 回答
0

我从上面的每个答案中提取了一点。我遇到了和你一样的问题。我得到的回报是从当前的 /blog/post/(media_url)/image.jpg

在我的管理门户中,我可以轻松查看并编辑它。但是在我的 post.html 上我遇到了问题,直到我添加了 {{ MEDIA_URL }} --

这就是我所缺少的。

我在下面发布了我的整个部分,以便其他人可以阅读它并查看他们缺少的内容。

post.html:

    <label for="id_image">  <img src="{{ MEDIA_URL }}{{ p.image.url }}" 
    title="{{ p.title }}"> </label>

models.py:

    from django.core.files.storage import FileSystemStorage

    upload_location =
    FileSystemStorage(location='/home/pi/djcode/xpcpro/xpcpro/images')

    class Blog(models.Model):
        title = models.CharField(max_length=255)
        author = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
        date = models.DateTimeField(auto_now=False, auto_now_add=True)
        body = models.TextField()
        image = models.ImageField(
            storage=upload_location, null=True,
            blank=True, width_field="width_field",
            height_field="height_field",)
        height_field = models.IntegerField(default=0)
        width_field = models.IntegerField(default=0)

urls.py:

    from django.conf.urls.static import static
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns

    urlpatterns += staticfiles_urlpatterns()
    urlpatterns += static(settings.MEDIA_URL,
    document_root=settings.MEDIA_ROOT)

settings.py:

    MEDIA_ROOT = "/home/pi/djcode/xpcpro/xpcpro/images/"
    MEDIA_URL = "/images/"
于 2016-07-08T01:28:00.640 回答