【问题标题】:CSS not working in some pages on DjangoCSS 在 Django 的某些页面中不起作用
【发布时间】:2015-11-12 19:53:31
【问题描述】:

我是 Python 和 Django 的初学者,一直在努力解决以下问题:

在 localhost:8000/ webiste 上很漂亮并且工作正常,但在 localhost:8000/invoice/ 上,一些引用不起作用。例如,CSS 不起作用,但图像和 JavaScript 可以。

我正在使用相对链接来指出我的路径在哪里。

    <script src="{% static "js/jquery.min.js" %}"></script>
    <script src="{% static "js/skel.min.js" %}"></script>
    <script src="{% static "js/skel-layers.min.js" %}"></script>
    <script src="{% static "js/init.js" %}"></script>

    <noscript>
        <link rel="stylesheet" href="{% static "css/skel.css" %}"/>
        <link rel="stylesheet" href="{% static "css/style.css" %}"/>
        <link rel="stylesheet" href="{% static "css/style-xlarge.css" %}"/>
    </noscript>

    <img src="{% static "images/pic07.jpg" %}" alt="" />

Settings.py

STATIC_URL = '/static/'

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

查看.py

def home(request):
  return render(request, 'principal.html',)

def invoice(request):
  return render(request, 'invoice.html',)

当我使用浏览器检查页面代码时,我看到链接被称为http://localhost:8000/invoice/static/css/somecss.css

invoice.html 在 /project/folder/templates/ 内,而我的 .css 在 /project/static/css/ 内。 invoice.html 位于我的 index.html (principal.html) 页面的同一个文件夹中,该页面正在运行。

此外,网站/发票/页面内的所有链接都变为网站/发票/链接而不是网站/链接

【问题讨论】:

  • 什么CSS?您的示例实际上没有显示任何内容...请提供minimal reproducible example
  • 其实你使用的是django框架,而不仅仅是“python”。 invoice.html 在哪里?
  • invoice.html 在 project/folder/templates/ 内,而我的 .css 在 /project/static/css/ 内。 invoice.html 位于我的 index.html 页面所在的同一文件夹中,该页面正在运行。
  • 我已经删除了

标签: python css django reference


【解决方案1】:

删除标签的问题已解决。同样对于由于 js 文件引用其他 css 文件而导致控制台上的 404 错误,我在模板中添加了一个变量以包含 url:

<script>
  var url = "{% static 'css/' %}";
</script>

在 .js 中我使用了 url.concat('style.css')

【讨论】:

    猜你喜欢
    • 2021-11-24
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多