STATIC_URL Not Working After Django 1.5 Upgrade

2019-03-01 03:01发布

问题:

Im probably just tired and dont notice something obvious here but after upgrade to Django 1.5 the path to my static files is broken.

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" />

回答1:

{% load static from staticfiles %}

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

Documentation for the new implementation of Django 1.5:

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



回答2:

Because in Django 1.5 you have to use {% load staticfiles %}

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