如何嵌入样式?

时间:2014-03-14 12:08:40

标签: python django

请帮忙整理一下。

我的问题是在html-template中添加django1.6 css-file是不可能的。我的项目目录结构如下:

proj1(catalog)
    manage.py(file)
    proj1(catalog)
        wsgi.py(file)
        urls.py(file)
        settings.py(file)
        __init__.py(file)
        views(catalog)
        templates(catalog)
            index.html(file)
        static(catalog)
            css(catalog)
                styles.css(file)

urls.py:

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


import proj1.views.views

urlpatterns = patterns('',   
    url('^$', proj1.views.views.hello),
    url('^datetime$', proj1.views.views.current_datetime),
    url('^dt$', proj1.views.views.current_datetime),
    url('^dt/(\d{0,2})$', proj1.views.views.current_datetime2),
    url('^dynamic$', proj1.views.views.dynamic),
)

urlpatterns += staticfiles_urlpatterns()
在settings.py中

规定了以下内容(摘录):

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'proj1/templates/'),
)
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)

index.html以下内容:

<!DOCTYPE html>
{% load static %}
<html>
    <head>
        <meta charset="utf-8" />

        <title>qwerty</title>

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

    <body>
        <div class="wrap">
            <span class='text'>hello,</span> <span class='name'>{{name}}</span>
        </div>
    </body>
</html>

由于所有这些本身对于某些页面出现url(html true),但是没有连接css -

PS windows7的

1 个答案:

答案 0 :(得分:0)

将BASE_DIR更改为以下

os.path.dirname(os.path.dirname(os.path.realpath(__file__)))

然后使用href作为

href="/static/css/style.css"