【问题标题】:Django static img not showing in my blogDjango 静态 img 未显示在我的博客中
【发布时间】:2014-04-23 18:46:25
【问题描述】:

首先让我说我是一个想学习 django 的学生... 我通过教程完成了我的应用程序,但我的模板中显示的图像有问题,它只是显示没有图像的框架......这是我的 html 代码:

            <aside>
                <h4>Historico</h4>
                {% for month in months %}
                <p><a href="{url 'blog.views.month' month.0 month.1}">{{ month.2 }}</a></p>

                {% endfor %}
                <h4>Sobre mi</h4>
                <img class="pic" src="{{ STATIC_URL }}/static/img/foto.jpg"  alt="foto"/>

                <img class="picbig" src="{{ STATIC_URL }}/static/img/foto.jpg"  alt="foto grande"/>
                <p>Soy un doge san OP platino IV lan pls</p>
            </aside>

我的图片“foto.jpg”没有显示,这是我的静态设置.py

STATIC_URL = '/static/'
STATICFILES_DIRS = (
"C:/Users/Abdul Hamid/PycharmProjects/misitioweb/static",
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',

)

我的img在这样的文件夹中

`C:\Users\User-Name\PycharmProjects\misitioweb\`

`blog-
-templtates
--frontpage.html
--otherthing.html
-__init__.py
-admin.py
-models.py
-test.py
-views.py
mysite-
-__init__.py
-settings.py
-urls.py
-wsgi.py
manage.py
-etc..
static-
--img
---foto.jpg
`

我一直在尝试更改 img 标记中的代码以获得不同的结果,例如将静态文件夹加载到模板中,如下所示: {{from static import staticfiles}}

我做错了什么,我已经尝试搜索更多信息,但没有任何效果...请帮助我! 反正 谢谢你的时间

【问题讨论】:

标签: python django image templates static


【解决方案1】:

看起来您的 src 属性对您的图像不正确。

<img class="pic" src="{{ STATIC_URL }}/static/img/foto.jpg"  alt="foto"/>

应该是

<img class="pic" src="{{ STATIC_URL }}img/foto.jpg"  alt="foto"/>

由于 STATIC_URL 已经负责将 /static/ 添加到您的 url。如果您在浏览器中使用调试器工具并查看该图像的 src,您可能会看到它显示“/static/static/img/foto.jpg”,而它应该是“/static/img/foto.jpg”。 jpg"

【讨论】:

    【解决方案2】:

    同时确保您已通过运行以下命令收集静态图像文件:

    python manange.py collectstatic
    

    【讨论】:

    • ImproperlyConfigured("您正在使用 staticfiles 应用程序" django.core.exceptions.ImproperlyConfigured: 您正在使用 staticfiles 应用程序而没有将 STATIC_ROOT 设置设置为文件系统路径。
    • 我应该在我的 settings.py 上的 static_root 中移动一些东西吗?
    • 在这里查看我的答案:stackoverflow.com/questions/23252056/…
    猜你喜欢
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    相关资源
    最近更新 更多