Django 1.5升级后STATIC_URL无法正常工作

时间:2013-03-03 09:22:21

标签: django django-settings django-staticfiles

我可能只是累了,并没有注意到这里显而易见的事情,但在升级到Django 1.5后,我的静态文件的路径被破坏了。

settings.py

from os.path import abspath, basename, dirname, join, normpath

SITE_ROOT = dirname(dirname(abspath(__file__)))

SITE_NAME = basename(SITE_ROOT)

PROJECT_ROOT = dirname(SITE_ROOT)

STATIC_ROOT = normpath(join(SITE_ROOT, 'static', 'site_media'))

STATIC_URL = "/site_media/static/"

STATICFILES_FINDERS = (
    "staticfiles.finders.FileSystemFinder",
    "staticfiles.finders.AppDirectoriesFinder",
    "staticfiles.finders.LegacyAppDirectoriesFinder",
    "compressor.finders.CompressorFinder",

的index.html

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

2 个答案:

答案 0 :(得分:9)

{% load static from staticfiles %}

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

Django 1.5新实现的文档:

https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#template-tags

答案 1 :(得分:1)

因为在Django 1.5中你必须使用{%load staticfiles%}

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