5

I am struggling to get css to work globally across my Django project.

I have a STATICFILES_DIRS called 'project_static' with a css file in it. This is in the root of my project. In my settings.py I have:

STATICFILES_DIRS = (
'/Users/gavinhinfey/django_projects/ss_stream/project_static/',
)

In base.html I have:

<link rel="stylesheet" href="{{STATIC_URL}}css/main.css">

{% block content %}
{% endblock content %}

The css file links fine when I'm at a page template within an app 'stream' which I have created. However when I'm viewing a page template not specific to an app it does not see the css.

Any idea why this is?

I've done my best to explain this but if you need clarification on the problem please ask.

Thanks Gavin

4

2 回答 2

4

{{STATIC_URL}}已被弃用(我认为)所以它可能css/main.css只是渲染。

我建议你配置如下:

设置.py

import os.path

PWD = os.path.dirname(os.path.realpath(__file__))  # project root path

STATICFILES_DIRS = (
    PWD + '/static/',  # or project_static, whatever
)

base.html

{% load static %}

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

通过这种方式,您可以在设置中使用相对路径,并避免在将整个项目移到主目录之外时破坏设置。

您可以将它用于每个路径设置,例如LOCALE_PATHSTEMPLATE_DIRS

如果这还不起作用,请检查您是否具有以下设置:

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    # 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# optional but if you defined it be sure to have this one:
TEMPLATE_CONTEXT_PROCESSORS = (
    # ...
    'django.core.context_processors.static',
    # ...
)
于 2013-08-12T15:06:26.353 回答
0

请注意,STATICFILES_DIRS 是一个可与目录交互,可在其中找到项目的静态文件。但应用程序不应在同一文件夹中查找文件。它应该在 STATIC_ROOT 中查找文件(这是不同的)。您的资源文件将仅在 STATIC_ROOT 之后manage.py collectstatic

于 2013-08-12T15:33:09.523 回答