【问题标题】:style.css file location in djangodjango 中的 style.css 文件位置
【发布时间】:2023-04-02 00:41:01
【问题描述】:

我有一个 style.css 文件,它在我的 Django 项目中的所有应用程序中都很常见。位置如下:

testsite______
              |
              |
           testsite
              |
              |
             news 
              |
              |
            reviews
              |
              |
           templates------>static------>css------> style.css
              |
              |
           manage.py

在项目的settings.py中,我有STATICFILES_DIRS如下:

STATIC_URL = '/static/'
STATICFILES_DIRS = (
        "/path/to/my/project/templates/static/",
)

我在模板头中调用css文件如下:

<html>
    <head>
        <title>TEst site</title>
        <link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/style.css"/>
    </head>

但是,css 没有加载。它给出了一个 404 错误,其中服务器在应用程序中查找 css 文件(新闻、评论)。错误如下:

"GET /test-site/css/style.css HTTP/1.1" 404 4147

我需要在 urls.py 中为 css 指定一些东西吗?我在这里错过了什么?

【问题讨论】:

  • INSTALLED_APPS 中有 django.contrib.staticfile 吗? more here
  • @karthikr - 是的,django.contrib.staticfileINSTALLED_APPS

标签: python html css django


【解决方案1】:

不,您不必在 url 中指定 css 的任何内容。 尝试放入 HTML

{% load staticfiles %}

并调用一个

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

我就是这样做的,效果很好。

【讨论】:

    猜你喜欢
    • 2020-01-05
    • 2011-08-18
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    相关资源
    最近更新 更多