【问题标题】:Serve static files with Django without STATIC_URL使用没有 STATIC_URL 的 Django 提供静态文件
【发布时间】:2016-07-27 14:02:30
【问题描述】:

您好,您有一个在设置中使用 STATIC_URL 的应用程序。例如:

STATIC_URL = '/static/'

这些在 url 模式中:

url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
        {'document_root': settings.MEDIA_ROOT}),

它在 html 中的用法是这样的:

<img src="{{STATIC_URL}}images/formshare-computer.png" width="497" height="363"/>

如果应用程序在 http://mydomin.org/ 中提供,则此方法有效,但如果我在 http://mydomin.org/myapp/ 中提供它,我需要更改 STATIC_URL

有没有办法在没有 STATIC_URL 的情况下提供静态文件或如何在 html 中正确使用它?

【问题讨论】:

    标签: django static-media


    【解决方案1】:

    来自documentation - 在您的模板中使用:

    {% load static %}
    <img src="{% static 'images/formshare-computer.png' %}" width="497" height="363"/>
    

    MEDIA_* 设置与您的静态内容无关,它们用于管理用户上传的内容。

    【讨论】:

    • 返回与 STATIC_URL 相同的值。如果 Django 应用程序使用 WSGI 运行,例如 localhost/myapp,那么如果使用 /static/ 则不存在。
    猜你喜欢
    • 2012-10-03
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 2016-01-02
    • 1970-01-01
    • 2020-01-29
    相关资源
    最近更新 更多