Django:CSS不是没有用

时间:2012-11-19 00:55:21

标签: python html css django web

我还是django的新手,我的CSS工作有问题 我已经按照链接Django Static Link tutorial的方向来处理静态文件。但它仍然无法正常工作。

设置

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = '/Users/a9austin/Development/sites/AlphaSocks/src/static_root/'

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
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.
'/Users/a9austin/Development/sites/AlphaSocks/src/staticfiles'

)

视图

#from django.http import HttpResponse
from django.shortcuts import render_to_response


def index(request):
return render_to_response('index.html')


的index.html

<link rel="stylesheet" href="{{STATIC_URL}}css/style.css" type="text/css" media="screen" >

目录组织

  

SRC-&GT; staticfiles-&GT; CSS-&GT; style.css中



非常感谢你,非常感谢你的帮助和时间!

7 个答案:

答案 0 :(得分:27)

对于Django提供静态文件,您必须确保有几个设置。

<强> STATIC_URL

此设置指定静态文件应映射到的URL。你已经完成了。

<强> STATICFILES_DIRS

这指定了系统中Django应该查找静态文件的所有文件夹。我们的想法是,您的项目中可能有几个应用程序,每个应用程序可能需要一组不同的静态文件。因此,出于组织目的,每个应用程序可能包含一个static目录,它只存储它的静态文件。那么Django必须有办法知道这些目录的位置。这就是这个设置的用途。

<强> STATIC_ROOT

此设置指定Django将所有静态文件复制到的位置,而不是静态文件的位置。这个想法是,一旦你将开发投入生产,Django就不能再提供静态文件了,因为我不会去这里(问题出在article)。但是对于生产,所有静态文件应该位于单个目录中,而不是像STATICFILES_DIRS中指定的那样。因此,此设置通过运行以下命令指定Django将从STATICFILES_DIRS中的所有文件复制所有静态文件的目录:

$ python manage.py collectstatic

请注意,这仅在您投入生产后才需要,并且此处指定的目录不能与STATICFILES_DIRS中指定的任何目录相同。

<强> Urls.py

在开发Django以提供静态文件时,您必须在urls.py中包含静态网址:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = ...

urlpatterns += staticfiles_urlpatterns()

完成上述所有操作后,只要您拥有DEBUG = True,就应该提供静态文件。在上面的列表中,您似乎只完成了STATIC_URL。另请注意,我在上面描述的所有步骤都在您在问题中链接的文档中(link)。它可能在开始时有点令人困惑,但如果你阅读它几次,它就会变得更加清晰。

答案 1 :(得分:1)

将RequestContext添加到响应中应该将STATIC_URL变量加载到模板中。

尝试更改:

from django.shortcuts import render_to_response

def index(request):
    return render_to_response('index.html')

为:

from django.shortcuts import render_to_response
from django.template.context import RequestContext

def index(request):
    return render_to_response("index.html", context_instance=RequestContext(request)) 

有关详细信息,请参阅Referring to static files in templates上的Django文档。

答案 2 :(得分:1)

完成所有操作后,将DEBUG = True设置为,python python collectstatic,清除高速缓存,如果问题仍然存在,则以隐身模式打开,将.css文件复制到静态文件夹中的另一个新.css文件中,然后运行collectstatic命令。这为我解决了。 希望对您有帮助。

答案 3 :(得分:1)

尝试清除缓存。如果您使用的是谷歌浏览器,请转到您的设置>清除浏览数据>选择清除缓存的图像和文件,然后点击清除数据

答案 4 :(得分:0)

There is an easy way if you feel that your CSS isn't working.

如果您的项目规模不算太大,那么您可以 CSS文件与HTML相同。 然后运行它,这样它将例如运行

`     

<head>
  <meta charset="UTF-8">
  <title>Promantus Bot</title>
      <style type="text/css"> 
* {
    margin: 0;
    padding: 0;
}

body {
    background-color:#FF625F;
}

h1, p {
    font-family: sans-serif;
    text-align: center;
    color: #323330;
    font-size:  100px;
}


p {
    font-size: 30px;
}

#output, #container {
    display: flex;
    justify-content: center;
    margin-top: 100px;
}


input {
    background-color: #eee;
    border: none;
    font-family: sans-serif;
    color: #000;
    padding: 15px 32px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 30px;
}






</style>


</head>

<body>

  <div id="output"></div>

<div id="container">
    <input type="text" id="input" value="">
</div>





</body>

</html>
`
It's going to run fine this way.

答案 5 :(得分:0)

如果编码没有问题并且没有显示错误。 然后你可以这样做来尝试解决问题。

清除缓存:

如果您使用的是谷歌浏览器,请转到您的设置 --> 清除浏览数据 --> 选择清除缓存的图片和文件,然后点击清除数据

答案 6 :(得分:0)

如果您在开发模式下遇到这种情况,请确保您在 DEBUG=True 文件中设置了 settings.py。还要确保 MEDIA_URLMEDIA_ROOT 在您的 settings.py 文件中设置如下:

MEDIA_URL  = '/mymediafolder/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'mymediafolder')

然后在您的主网址文件 myapp/urls.py 中,您必须具有以下内容:

from django.conf.urls import url, include
from . import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from django.conf import settings

urlpatterns = [
    #Your url patterns here
]

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

staticfiles_urlpatterns() 用于在开发模式下提供静态文件。