【发布时间】:2017-03-05 00:23:48
【问题描述】:
我已经尽可能地查看了所有内容,但我没有找到我遇到的问题的答案。我在这上面花了几个小时,发现没有用。
我的前端在项目下或应用树下使用静态文件夹显示所有图像和 css。
在我进行更改以使用动态 url,并从项目文件夹(向下一个文件夹)中创建静态文件夹后,collectstatic 突然间我的图像消失了,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")
---|url.py
if settings.DEBUG == True:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
---|main.html(模板)
{% load staticfiles %}
<img src={% static "images/banner_top.png" %}>
或
<img src="..static/images/banner_top.png">
或
<img src="static/images/banner_top.png">
在浏览器上图像不显示,但后端显示 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
这是标题
一般
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
请求
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
【问题讨论】:
标签: python html django image django-templates