使用Django 1.5和Twitter Bootstrap

3
我将尝试将Twitter Bootstrap与我的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.
    "/path/to/my/projects/templates/static/",
)

static 文件夹下,有三个文件夹,分别是css、img 和 js,并且所有的 Bootstrap 文件都已按原样复制到其中。
我的模板如下:
<html>
    <head>
        {% load staticfiles %}
        <link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap-responsive.css' %}" />
        <script type="text/javascript" src="{% static 'js/bootstrap.js' %}"></script>
        <meta charset="utf-8"> 

        <title>Test App</title>

    </head>
    <body>
        <div class="navbar navbar-fixed-top">  
            <div class="navbar-inner">  
                <div class="container">  
                    <ul class="nav">  
                        <li class="active">  
                            <a class="brand" href="#">TEST APP</a>  
                        </li>  
                        <li><a href="#">About</a></li>  
                        <li><a href="#">Portfolio</a></li>  
                        <li><a href="#">Contact</a></li>  
                    </ul> 
                </div>  
            </div>  
        </div>

然而,当我运行开发服务器时,我得到一个基本的HTML页面,没有任何变化和应用的CSS。

我在这里做错了什么?


你是在使用Windows、Mac还是Linux操作系统?你的IDE是什么? - Games Brainiac
@GamesBrainiac - Fedora 19和Eclipse配合Pydev使用 - rahuL
然后看一下我给你展示的代码。那应该可以解决你的问题。 - Games Brainiac
你是否已经配置了Django以为你提供静态文件服务? - Density 21.5
1个回答

2
假设您的 static 文件夹直接位于应用程序根目录下,以下是一种方法,可确保在所有操作系统上都能安全地渲染静态文件模板。
import os

def replace(path):
    assert isinstance(path, str)
    return path.replace('\\', os.sep)


def here(*args):
    return replace(os.path.abspath(os.path.join(os.path.dirname(__file__), *args)))


BASE_DIR = here('..')


def root(*args):
    return replace(os.path.abspath(os.path.join(BASE_DIR, *args)))

STATICFILES_DIRS = (root('static'),)

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