【问题标题】:Wrong formation of static links in template django模板 django 中静态链接的错误形成
【发布时间】:2017-11-24 02:53:47
【问题描述】:

我使用 nginx 部署了我的 Django 应用程序并且工作正常。但是我的图像没有加载到页面中。静态文件夹不是问题,因为成功加载了 favicon 和 css 和 js 文件。

我查看了在 webbrowser 中形成的源代码,我看到图像在链接末尾有一个“/”。这使地址无效。如果手动删除“/”,我在站点中找到了资源。但我不知道是什么导致了这个字符的插入。

我的网站可用文件

server {
    listen 80;
    server_name localhost;

    location = /favicon.ico {access_log_off; log_not_found off;}
    location /static/ {
        root /home/pi/WebSite;
    }
    location / {
        include proxy_params;
        proxy_pass http://unix:home/pi/WebSite/Website.sock;
    }
}

我的设置.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static/')

我的模板文件:

<div class='col-md-12 col-xs-12'>
   <img class="img-rounded img-responsive text-center" src = {% static 'img/sistema.png' %}/>
   <img class="img-rounded img-responsive text-center" src = {% static 'img/legenda.png' %}/>
</div>

在网站的源代码中,图片出现:

<div class='col-md-12 col-xs-12'>
   <img class="img-rounded img-responsive text-center" src = /static/img/sistema.png/>
   <img class="img-rounded img-responsive text-center" src = /static/img/legenda.png/>
</div>

有什么想法、建议吗? 非常感谢,我没有更多的想法

【问题讨论】:

  • 我的错......我在错误的模式下使用了 img html 语句。 img 没有以“/>”结尾。抱歉,在开发模式下,页面工作正常 rsrs。

标签: django nginx static web-deployment gunicorn


【解决方案1】:

您正在使用src = {% static 'img/sistema.png' %}。这会导致 HTML 无效。相反,您应该拥有src="{% static 'img/sistema.png' %}"。这意味着浏览器会知道/ 不是图像 URL 的一部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 2012-11-15
    • 1970-01-01
    相关资源
    最近更新 更多