Django静态图像不显示

And*_*ndy 6 html python django image django-templates

我已经尽可能地查看了所有内容,但没有找到我遇到的问题的答案。我花了几个小时在这,发现无济于事。

我的前端在项目下或应用程序树下使用静态文件夹显示所有图像和 css。

在我进行更改以使用动态 url 并从项目文件夹(一个文件夹向下)创建静态文件夹后,我的图像突然消失了,css 仍然可以直接路径或动态工作。

我已经对图像进行了相同的尝试,直接路径(因为文件夹的副本仍然保留在项目文件夹中)直接路径不起作用,动态路径也不起作用。

它也不返回 404,在第一次加载时,它返回 200,在第二次 304 时返回,就好像图像在屏幕上一样,但它不是。只是没有错误,但不存在该死的图像。让我感到困惑的是 css 可以工作,但图像不显示。

波纹管是我的代码。

---|settings.py

STATIC_URL = '/static/'

STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),
  #'/var/www/static/',
]

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static_cdn")
Run Code Online (Sandbox Code Playgroud)

---|url.py

if settings.DEBUG == True:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Run Code Online (Sandbox Code Playgroud)

---|main.html(模板)

{% load staticfiles %}
 <img src={% static "images/banner_top.png" %}>
Run Code Online (Sandbox Code Playgroud)

或者

<img src="..static/images/banner_top.png">
Run Code Online (Sandbox Code Playgroud)

或者

<img src="static/images/banner_top.png">
Run Code Online (Sandbox Code Playgroud)

在浏览器上,图像不显示,但后端显示 200 和图像的正确路径。

http://127.0.0.1:8000/static/images/banner_top.png

[22/Oct/2016 22:32:10] "GET /static/images/banner_top.png HTTP/1.1" 200 0
Run Code Online (Sandbox Code Playgroud)

这是标题

一般的

Request URL:http://127.0.0.1:8000/static/images/banner_top.png
Request Method:GET
Status Code:200 OK
Remote Address:127.0.0.1:8000
Run Code Online (Sandbox Code Playgroud)

要求

 Content-Length:0
 Content-Type:image/png
 Date:Sun, 23 Oct 2016 08:06:17 GMT
 Last-Modified:Fri, 21 Oct 2016 06:54:06 GMT
 Server:WSGIServer/0.1 Python/2.7.10
Run Code Online (Sandbox Code Playgroud)

And*_*ndy 3

嗯,问题已经解决了。#django IRC 上的 Koterpillar 帮助我解决了这个问题。

不知何故,系统清空了图像文件(已损坏)(我认为这是通过收集静态发生的),这就是加载图像但没有显示任何内容的原因。

经检查

curl -D - http://127.0.0.1:8000/static/images/banner_top.png
Run Code Online (Sandbox Code Playgroud)

它只返回标题,没有内容。因此,接下来检查图像文件并验证内容是否已消失。我替换为新图像,然后在网站重新加载时再次显示。

非常感谢 Koterpillar