Django - 在基础模板中处理静态文件

9

如何让基础模板使用静态文件,并使其他继承基础模板的模板使用相同的静态文件?

根据Django文档,它介绍了如何使用特定应用程序所使用的静态文件。但是我似乎找不到一种方法来使用来自应用程序外部的静态文件。

给定的应用程序继承所有内容,但不包括静态文件。

我的settings.py:

STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.

)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
INSTALLED_APPS = (    
'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'south',
    'myproject.apps.finance',
    'myproject.apps.base',
    'django.contrib.admin',
  )

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.contrib.auth.context_processors.auth',
    'django.contrib.messages.context_processors.messages',
)

这是我的当前主页设置:
主页的 URL:
from django.conf.urls import patterns, include, url
from myproject.views import hello

urlpatterns = patterns('',
   ('', hello),
)

首页视图:

from django.shortcuts import render_to_response
from django.http import Http404, HttpResponse
import datetime

def hello(request):
    hello = "Hello World"
    return render_to_response('home/home.html', {'hello': hello})

主页模板:

{% extends "base/base.html" %}

{% block title %} My Homepage {% endblock %}
{% block content %}
<p>I say {{ hello }}</p>

{% endblock %}

基础模板:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <link rel="stylesheet" href="{{ STATIC_URL }}/css/style.css">
    <title>{% block title %}{% endblock %}</title>
</head>
<body>
    <h1>My Personal Finance Site</h1>
    {% block content %}{% endblock %}
    {% block footer %}
    <section class="divider1">
    <p>Thanks for visiting my site.</p>
    <p>All rights reserved</p>
    </section>
    {% endblock %}
</body>
</html>

我的CSS文件位于名为base的空项目中。但我认为可以更好地使用在给定应用程序之外的静态文件。
那么,将基本模板与CSS文件链接起来的最佳方法是什么,以使继承基本模板的其他模板获得相同的CSS文件配置?
2个回答

7

base.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    {% block css %}
    <link rel="stylesheet" href="{{ STATIC_URL }}/css/style.css">
    {% endblock %}
    <title>{% block title %}{% endblock %}</title>
</head>
<body>
    <h1>My Personal Finance Site</h1>
    {% block content %}{% endblock %}
    {% block footer %}
    <section class="divider1">
    <p>Thanks for visiting my site.</p>
    <p>All rights reserved</p>
    </section>
    {% endblock %}
</body>
</html>

page.html

{% extends "base/base.html" %}

{% block title %} My Homepage {% endblock %}

{% block css %}{{block.super}}
//put css here
{% endblock %}

{% block content %}
<p>I say {{ hello }}</p>

{% endblock %}

urls.py

from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

from project_name import settings

admin.autodiscover()

urlpatterns = patterns('',
   .......
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += staticfiles_urlpatterns()

settings.py

import os
import sys

..........................

SITE_ROOT = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(SITE_ROOT, 'app_name'))

MEDIA_ROOT = os.path.join(SITE_ROOT, 'media')
MEDIA_URL = '/media/'

STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
   # Put strings here, like "/home/html/static" or "C:/www/django/static".
   # Always use forward slashes, even on Windows.
   # Don't forget to use absolute paths, not relative paths.
   os.path.join(SITE_ROOT, 'staticfiles'),
)

TEMPLATE_DIRS = (
   # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
   # Always use forward slashes, even on Windows.
   # Don't forget to use absolute paths, not relative paths.
   os.path.join(SITE_ROOT, 'templates'),
)

....................

你的代码出现了错误。这是因为 block.super 没有闭合标签。另外,当 base.html 没有活动应用程序时,它怎么知道在哪里找到 static_url 呢?难道不应该有一种方法在应用程序之外定位这些静态文件吗? - Andrius
实际上是因为您的{{endblock}}写错了,应该是{% endblock %}。此外,它没有起作用,不能与CSS文件链接。 - Andrius
抱歉,我的 CSS 结束块是错误的,但我现在已经更新了。要定位文件,只需使用 {{STATIC_URL}}。 - catherine
好的,答案已更新。通过在您的settings.py中配置文件的确切位置来获取答案。 - catherine
让我们在聊天中继续这个讨论 - Andrius
显示剩余7条评论

-1
请确保模板标签 "{% load static %}" 位于您的 .html 文件顶部。这将加载您的静态文件夹。
示例
{% load static %}
{% extends "base/base.html" %}

{% block title %} My Homepage {% endblock %}

{% block css %}{{block.super}}
//put css here
{% endblock %}

{% block content %}
<p>I say {{ hello }}</p>
{% endblock %}

这是不正确的。它会告诉你,在模板中,扩展"base.html"必须是第一个标签。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接