【问题标题】:Django: Wish to have my css file located in my project instead of an appDjango:希望我的css文件位于我的项目而不是应用程序中
【发布时间】:2015-03-20 21:01:30
【问题描述】:

我想将我的 style.css 文件保存在 /static/ 或 /templates/ 下的主项目文件夹(对于我在 django 中的项目),但我似乎无法正确加载它。我用:

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

这个 css 文件应该与我的 /templates/base.html 站点一起加载(存储在主项目文件夹中)。

谢谢, 马特

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    templates 目录加载静态文件是个坏主意 - 您的模板源将可供网络用户使用。

    要从static/ 目录加载文件,请将STATICFILES_DIRS 添加到您的settings.py

    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, "static"),
    )
    

    顺便说一句,删除{% static %} 标签中的第一个斜线:

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

    【讨论】:

      猜你喜欢
      • 2012-07-09
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 2019-06-13
      • 2023-02-16
      • 2021-05-30
      相关资源
      最近更新 更多