【问题标题】:django not found static files in the subfolder of staticdjango 在 static 的子文件夹中找不到静态文件
【发布时间】:2014-07-30 18:17:28
【问题描述】:

django 1.6.5

我的静态文件在 static 的子文件夹下。

djangoprj
    djangoprj
        settings.py
        urls.py
        wsgi.py
        __init__.py
    static
        myapp
            mystaticfiles
            ...
    apps
        myapp
            ...
            templates
                *.html
            ...

在我的模板文件中,我将静态文件链接为完整路径。

<img src="/static/myapp/images/my.png" />

但是没有找到那些文件。我收到 404 错误。

当将 settings.py 更改为

STATIC='/static/myapp/'

我可以得到那些静态文件。但我不想那样做。当STATIC='/static/' 时,为什么不炒锅?有没有一些简单的方法来解决这个问题,而不是手动执行命令,例如“collectstatic”?我已将静态文件放在static 文件夹中,并且在模板文件中使用了完整路径。为什么它不起作用?提前致谢。

【问题讨论】:

    标签: python django static


    【解决方案1】:

    现在您将静态文件夹切换到'/static/myapp/'

    你应该在你的标签中使用&lt;img src="/static/images/my.png" /&gt;

    通常,您应该使用{% static %} 模板标签来执行此操作,如下所示(假设STATIC='/static/')。

    {% load staticfiles %}
    
    <img src="{% static 'images/myapp/my.png' %}" />
    

    要了解更多信息,请参阅教程,https://docs.djangoproject.com/en/1.6/intro/tutorial06/

    我建议您阅读https://docs.djangoproject.com/en/1.6/中的所有教程(第1部分-第6部分)

    这样你就可以对基础有足够的了解。

    【讨论】:

    • &lt;img src="{% static 'images/myapp/my.png' %}" /&gt; 应该是 myapp/images/my.pngHERE = os.path.dirname(os.path.dirname(__file__)) STATICFILES_DIRS = ( os.path.join(HERE, "static"), ) 在 settings.py 中也可以。
    【解决方案2】:

    您也可以在这些答案中找到一些帮助

    1. static files problem
    2. STATIC_URL Not Working

    在我的配置中,我有一个目录结构,其中静态文件位于我的应用程序中,如下所示

    djangoprj
        djangoprj
            settings.py
            urls.py
            wsgi.py
            __init__.py
        apps
            myapp
                ...
                templates
                    *.html
                static   
                    myapp
                       mystaticfiles
                ...
                ...
    

    在设置中

    INSTALLED_APPS = (
        ...
        'django.contrib.staticfiles',
        ...
    )
    
    STATIC_URL = '/static/'
    

    在模板中我使用“静态”标签

    {% load staticfiles %}
    <link rel="stylesheet" type="text/css" href="{% static 'posts/style.css' %}" />
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-18
      • 2016-04-01
      • 2020-06-14
      • 2020-06-03
      • 2018-11-16
      • 1970-01-01
      • 1970-01-01
      • 2021-08-27
      相关资源
      最近更新 更多